Question: Inheritance from a buil-in type
Asun Friere
afriere at yahoo.co.uk
Tue Sep 30 04:22:05 EDT 2003
Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote in message news:<Xns9405A01F17467duncanrcpcouk at 127.0.0.1>...
>
> There are two rules. Generally, immutable objects get their initial values
> from the constructor (__new__) while mutable objects are constructed with a
> default value (e.g. empty list or dict) and are then set by the initialiser
> (__init__) method. A few types which you might expect to be immutable are
> actually mutable (e.g. property).
>
What is the thinking behind that? I mean you /can/ pass initial values
to a mutable using __new__, eg
class MyList (list) :
def __new__(cls, *args, **kwargs) :
return list.__new__(cls, *args, **kwargs)
or __init__ to pass values to a newly created immutable, eg
class MyTuple (tuple) :
def __init__(self, *args, **kwargs) :
return super(tuple, self).__init__(*args, **kwargs)
can't you? What's the pitfall?
More information about the Python-list
mailing list