Question: Inheritance from a buil-in type

Pettersen, Bjorn S BjornPettersen at fairisaac.com
Tue Sep 30 04:47:45 EDT 2003


> From: Asun Friere [mailto:afriere at yahoo.co.uk] 
> 
> 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?

Shart answer: you should call super as "super(MyTuple, self)".

Long answer: http://www.python.org/2.2/descrintro.html#cooperation.

-- bjorn






More information about the Python-list mailing list