By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

__init__ in subclass of tuple

P: n/a
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/Coo.../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
Mar 10 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
En Sat, 10 Mar 2007 02:36:41 -0300, Alan Isaac <ai****@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/Coo.../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:

pytuple.__init__
<slot wrapper '__init__' of 'object' objects>

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

pytuple.__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

Mar 10 '07 #2

P: n/a
"Gabriel Genellina" <ga*******@yahoo.com.arwrote:
The signature is like you said, but it's not a tuple method, it's an
object method instead:
pytuple.__init__
<slot wrapper '__init__' of 'object' objects>
The only important thing is that it says: of 'object' objects, not: of
'tuple' objects. Compare with:
pytuple.__len__
<slot wrapper '__len__' of 'tuple' objects>

Thanks for that clue!
Alan

Mar 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.