473,379 Members | 1,355 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,379 software developers and data experts.

consequences of not calling object.__init__?

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):
def __init__(self, x):
super(C, self).__init__()
self.x = x

Looking at:

http://www.python.org/2.2.3/descrintro.html#__new__
"The built-in type 'object' has a dummy __new__ and a dummy __init__"

seems to suggest that the super call here is unnecessary. It's also not
made in the Super class example from that document:

http://www.python.org/2.2.3/descrint...l#superexample

I'm trying to get in the habit of calling super in all __init__ methods,
but it seems like it's unnecessary when the only superclass is object.
Assuming that the base class of C doesn't get changed from object, are
there consequences of not making this call?
Steve
Jul 18 '05 #1
6 3684
in the code that follows, instances of E haven't been through D's
rigorous initiation process

.. class C(object):
.. def __init__(self):
.. print "C"
..
.. class D(object):
.. def __init__(self):
.. print "D"
.. super(D, self).__init__()
..
.. class E(C, D):
.. def __init__(self):
.. print "E"
.. super(E, self).__init__()

Jul 18 '05 #2
Steven Bethard wrote:
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):
def __init__(self, x):
super(C, self).__init__()
self.x = x

Looking at:

http://www.python.org/2.2.3/descrintro.html#__new__
"The built-in type 'object' has a dummy __new__ and a dummy __init__"

seems to suggest that the super call here is unnecessary. It's also not
made in the Super class example from that document:

http://www.python.org/2.2.3/descrint...l#superexample

I'm trying to get in the habit of calling super in all __init__ methods,
but it seems like it's unnecessary when the only superclass is object.
Assuming that the base class of C doesn't get changed from object, are
there consequences of not making this call?

The principal one that I can see is that you are relying on this
implementation feature to maintain forward compatibility, since I'm not
aware of any pronouncement that says "object will *always* have a dummy
__init__".

There's also the possibility that you might want to use a different base
class later (for example, setting

object = mySuperDebugObject

for debugging purposes). If that object has an __init__() method you'll
have to put the calls in then anyway.

Perhaps a relevant question is how long it takes to call the __init__
method using super.

sholden@dellboy ~/Projects/PyCON2005
$ python /usr/lib/python2.4/timeit.py -s "
class C(object):
def __init__(self, x):
self.x = x" "C(1)"
100000 loops, best of 3: 2.69 usec per loop

sholden@dellboy ~/Projects/PyCON2005
$ python /usr/lib/python2.4/timeit.py -s "
class C(object):
def __init__(self, x):
super(C, self).__init__()
self.x = x" "C(1)"
100000 loops, best of 3: 5.58 usec per loop

So, even on my cronky old 1.3 GHz laptop [1] you only lose 3
microseconds per object creation. You'll have to decide how significant
that is.

regards
Steve

[1]: Freaky - I had just typed this when the doorbell went, and it was
the UPS driver delivering the new laptop!
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #3
John Lenton wrote:
in the code that follows, instances of E haven't been through D's
rigorous initiation process

. class C(object):
. def __init__(self):
. print "C"
.
. class D(object):
. def __init__(self):
. print "D"
. super(D, self).__init__()
.
. class E(C, D):
. def __init__(self):
. print "E"
. super(E, self).__init__()


Ahh, there's the example I was looking for. =)

Thanks!

Steve
Jul 18 '05 #4
Steven Bethard wrote:
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):
def __init__(self, x):
super(C, self).__init__()
self.x = x

Looking at:

http://www.python.org/2.2.3/descrintro.html#__new__
"The built-in type 'object' has a dummy __new__ and a dummy __init__"

seems to suggest that the super call here is unnecessary. It's also not
made in the Super class example from that document:

http://www.python.org/2.2.3/descrint...l#superexample

I'm trying to get in the habit of calling super in all __init__ methods,
but it seems like it's unnecessary when the only superclass is object.
Assuming that the base class of C doesn't get changed from object, are
there consequences of not making this call?


Yes!

Consider what happens if you multi-subclass from the above C class and
another class D.

class E(C, D):
def __init__(self, x):
super(E, self).__init__(x)
# some initialization for E

Now E.__mro__ is (E,C,D,object). So:

1. E's __init__ should call C's __init__ (this happens due to super call
in E.__init__)

2. C's __init__ should call D's __init__ (*this is why you need the
super call in C.__init__*)

Without it, D.__init__ will not be called. Note that D.__init__ should
not take any parameters in this case. Parameter similarity may be an
issue in call-next-method technique.

However, if you know you will not mutli-subclass from C, you may leave
out the super call.

HTH,
Shalabh
Jul 18 '05 #5
Steve Holden wrote:
The principal one that I can see is that you are relying on this
implementation feature to maintain forward compatibility, since I'm not
aware of any pronouncement that says "object will *always* have a dummy
__init__".


Maybe there's no such pronouncement, but unless there is a
clear statement somewhere (and I believe I've missed it, if
there is) that reads "one should *always* call __init__ on the
superclass even if one is just subclassing object and not
dealing with multiple inheritance situations", then I would
submit that the majority of Python code written using new-style
classes would be broken should what you suggest above ever
actually happen... starting with much of the code in the
standard library (based on a quick glance at those modules
whose contents match the re pattern "class .*(object):" .

-Peter
Jul 18 '05 #6
Peter Hansen, Quarta 29 Dezembro 2004 01:04, wrote:
Maybe there's no such pronouncement, but unless there is a
clear statement somewhere (and I believe I've missed it, if
there is) that reads "one should *always* call __init__ on the
superclass even if one is just subclassing object and not
dealing with multiple inheritance situations", then I would
submit that the majority of Python code written using new-style
classes would be broken should what you suggest above ever
actually happen... starting with much of the code in the
standard library (based on a quick glance at those modules
whose contents match the re pattern "class .*(object):" .


Things are kind weird at this point, since there are too many things to
think about and to make a decision on what should be done and what is
recommended to be done...

Quoting from http://www.python.org/peps/pep-0008.html:

"""
(...)
Designing for inheritance
(...)
Also decide whether your attributes should be private or not.
The difference between private and non-public is that the former
will never be useful for a derived class, while the latter might
be. Yes, you should design your classes with inheritence in
mind!
(...)
"""

So, I don't really know which is correct: to always call the constructor of
the parent class or just do that when it is needed by design...

I think that based on the above quotation from PEP-0008 code in the standard
library should be calling the parent class constructor. But then, I'm one
of the people who never do that :-)

--
Godoy. <go***@ieee.org>

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
1
by: MAF | last post by:
Is there anyway that I can find out what object called another object? ObjectABase ObjectA inherits from ObjectABase objectA calls objectB I want to invoke a method in ObjectABase?
4
by: Alan LeHun | last post by:
Is it possible for a subroutine to get a reference to the object instance that has called it? IOW, class1 creates an instance of class2 and calls one if its subroutines and I would like that...
6
by: Mirek Endys | last post by:
Hello all, another problem im solving right now. I badly need to get typeof object that called static method in base classe. I did it by parameter in method Load, but i thing there should be...
7
by: vsr | last post by:
I am calling one Method in Common class from different classes and i want to know from which object the call is coming from.. is there any way?
0
by: stormist | last post by:
I utilize a COM library to receive historical quote data on currencies. My problem is the constructors of the event are fixed. m_HistoryLookupClass.MinuteReceived += new...
0
by: Evan Klitzke | last post by:
Hi list, I was reading this article: http://fuhm.net/super-harmful/ and didn't understand the comment about calling super(Foo, self).__init__() when Foo inherits only from object. Can someone on...
1
by: vlmcntrl | last post by:
I'm building a website leaning heavily on js and ajax. I am trying to make the js object oriented which is fine until I get to ajax. I'm looking for a way to call my ajax onreadystatechange in the...
1
by: Maese Fernando | last post by:
Hi, I'm getting an odd error while trying to call the __init__ method of a super class: BaseField.__init__(self) TypeError: unbound method __init__() must be called with BaseField instance...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.