473,791 Members | 3,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__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,s eq):
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,s eq):
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 3486
En Sat, 10 Mar 2007 02:36:41 -0300, Alan Isaac <ai****@america n.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,s eq):
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,s eq):
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*******@yaho o.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
2217
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): .... tuple.__init__(self, (a,b) ) .... >>> a=pair(1,2) Traceback (most recent call last):
2
9646
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. (e.g., a security-restricted version, or an interface implementation that doesn't require a filesystem.) One (common?) exception seems to occur in initialization. I understand stripping out arguments that your subclass explicitly handles or...
3
1601
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 the logic and get different objects/instances by subclassing with appropriate names. Consider the following: ATTRS = {'one':, 'two':,
6
3717
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 C(object):
1
3261
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 pickle it? Thanks , Fedor
14
6409
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 is the right and common case to call init: class Mother(object): def __init__(self, param_mother): print 'Mother'
18
7394
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 in python, without copying the base class instance, which in my case is a very expensive object. Any ideas? Thanks,
8
1765
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 would be tempting but incorrect to call this the constructor of the class. It's tempting, because it looks like a constructor (by convention, __init__ is the first method defined for the class), acts like one (it's the first piece of code executed in...
4
3479
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
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.