__init__ in subclass of tuple

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Mar 10 01:25:18 EST 2007


En Sat, 10 Mar 2007 02:36:41 -0300, Alan Isaac <aisaac at american.edu>  
escribió:

> I am probably confused about immutable types.
> But for now my questions boil down to these two:
>
> - what does ``tuple.__init__`` do?

Nothing. tuple.__init__ does not even exist, as tuples are immutable, they  
are fully initialized with __new__ (the actual constructor)

> - what is the signature of ``tuple.__init__``?

Already said; it does not exist.

> These questions are stimulated by
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439
> Looking at that, what fails if I leave out the following line? ::
>
>     tuple.__init__(self)
>
> For exaple, if I try::
>
>     class Test1(tuple):
>         def __init__(self,seq):
>             pass
>
> I seem to get a perfectly usable tuple.
> What initialization is missing?

Nothing!

> Next, the signature question.
> I'm guessing the signature is something like
> tuple.__init__(self, *args, **kwargs)
> with nothing done with anything but self.
> As a trivial illustrative example::
>
>     class Test2(tuple):
>         def __init__(self,seq):
>             tuple.__init__(self, "foo", "bar")
>
> seems to cause no objections.

And does nothing; the tuple is already constructed (try with "print self"  
before the tuple.__init__ call).
(That recipe is a bit old, anyway, I think that tuples *never* have used  
__init__)
Better look for other "named tuples"/"record"/"struct" recipes on the  
CookBook.

The signature is like you said, but it's not a tuple method, it's an  
object method instead:

py> tuple.__init__
<slot wrapper '__init__' of 'object' objects>

The only important thing is that it says: of 'object' objects, not: of  
'tuple' objects. Compare with:

py> tuple.__len__
<slot wrapper '__len__' of 'tuple' objects>

> One last question: where should I have looked
> to answer these questions?

Uhm... Python Language Reference, section 3.4.1
Python/C API Reference Manual, section 10.3, Type objects.
The C source code, object.c
Or asking here :)

-- 
Gabriel Genellina




More information about the Python-list mailing list