473,836 Members | 2,243 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 3492
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
2219
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
9656
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
1605
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
3719
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
3266
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
6414
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
7400
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
1766
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
3481
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
9811
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10822
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10532
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10577
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
10241
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
9359
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
7774
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
6975
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();...
2
4003
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.