__init__ in subclass of tuple

A

Alan Isaac

I am probably confused about immutable types.
But for now my questions boil down to these two:

- what does ``tuple.__init__`` do?
- what is the signature of ``tuple.__init__``?

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?

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.

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

Thanks,
Alan Isaac
 
G

Gabriel Genellina

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__
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 :)
 
A

Alan Isaac

Gabriel Genellina said:
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>




Thanks for that clue!
Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top