473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

__init__ in subclass of tuple

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
2 3464
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Simon Burton | last post by:
Python 2.2.2 (#2, Nov 24 2002, 11:41:06) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class pair(tuple): .... def __init__(self,a,b): .... ...
2
by: Jim Jewett | last post by:
Normally, I expect a subclass to act in a manner consistent with its Base classes. In particular, I don't expect to *lose* any functionality, unless that was the whole point of the subclass. ...
3
by: paul kölle | last post by:
Hi list, in the course of writing a small app, I tried to design a class, which would allow to derive its behaviour solely from its name, so that I would be able to write one abstract class with...
6
by: Steven Bethard | last post by:
So when I'm writing a class and I define an __init__ method, I sometimes haven't called object.__init__, e.g.: class C(object): def __init__(self, x): self.x = x instead of class...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
14
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
18
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
8
by: kelin,zzf818 | last post by:
Hi, Today I read the following sentences, but I can not understand what does the __init__ method of a class do? __init__ is called immediately after an instance of the class is created. It...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.