Andrew MacKeith a écrit :
Thanks for the explanation, Bruno.
I have been successfully using a class derived from str, and in that
class I add a lot of extra data to the derived class instance in the
__init__ method of the derived class, so it is possible to mutate the
derived class __dict__, even if the base class data remains immutable.
Indeed. What I said was that Python builtins immutable types didn't use
the initializer, not that you couldn't use an initializer in derived
classes. I gave an example using __new__ because, quite often when
subclassing immutable types, peoples want to access the initial value
_before_ the call to the base class.
See example below.
The __init__ must have the same arguments as the base class.
The initializer (__init__) must have the same arguments as the
constructor (__new__). This is true for all and every classes.
(snip)
But you can get bitten if you do something that returns a new object
Traceback (most recent call last):
You need to override a couple other __magic_methods__ to make this work.
You'll find relevant documentation here:
http://docs.python.org/reference/datamodel.html#special-method-names
In the above case, you want to override at least the __add__ method:
class Y(str):
def __new__(cls, value, foo="foo"):
instance = str.__new__(cls, value)
instance.foo = foo
return instance
def __repr__(self):
return "<%s(%s, %s)>" % (type(self).__name__, self, self.foo)
def __add__(self, other):
return type(self)(str(self) + other, self.foo)
HTH