473,795 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__init__() not called automatically

hi,
i come from a c++ background. i ws happy to find myself on quite
familiar grounds with Python. But, what surprised me was the fact that
the __init__(), which is said to be the equivlent of the constructor in
c++, is not automatically called. I'm sure there must be ample reason
for this. I would like to know why this is so? This is my view is more
burden on the programmer.
Similarly, why do we have to explicitly use the 'self' keyword
everytime?

Every kind of help would be welcome.

Jul 19 '05
21 12294
On 25 May 2005 21:31:57 -0700,
"Sriek" <sc*******@gmai l.com> wrote:
Similarly, why do we have to explicitly use the 'self' keyword
everytime?


Why do they (the C++ programmers) prepend "m_" to otherwise perfectly
good member names?

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
Jul 19 '05 #11
"Sakesun Roykiattisak" <sa*****@boonth avorn.com> wrote in message
news:ma******** *************** *************** @python.org...
Does c++ call base class constructor automatically ??
If I'm not wrong, in c++ you also have to call base class constructor
explicitly.


In C++, if you don't call a base-class constructor (I am saying "a" rather
than "the" because there might be more than one direct base class), it is
called automatically with no arguments. The only case in which you must
call it explicitly is if it will not accept an empty argument list.
Jul 19 '05 #12
Sriek wrote:
maybe like this:
we can have the default behaviour as calling the default constructor
( with default arguements where required ). Along with this, keep the
option open to call constructors explicitly.


Ok, so here's another example:

def init(self):
print "An __init__ method, but in what class?!!"
print "Oh, this class: %s" % type(self).__na me__

class C(object):
pass

C.__init__ = init

So how would the compiler know that init() is a constructor for the C
object? (You can figure that out at runtime, but I don't see how you can
generally figure that out at compile time.)

STeVe
Jul 19 '05 #13
I guess you are right.

Jul 19 '05 #14
The compiler also calls the default arguement constructor
automatically, if such a constructor is provided for the base
class(es); but, this becomes a special case of what has been said by
Andrew Koenig.

So, it is NOT just the no arguement constructor that is automatically
called; note that the default arguement constructor is kinda put in
place because sometimes, the user may call the constructor with no
arguements, while the object actually needs some values for proper
construction.

Jul 19 '05 #15
Paul McNett wrote:
Sriek wrote:

(snip)
Similarly, why do we have to explicitly use the 'self' keyword
everytime?

This is closer to a wart, IMO,


I've always explicitelly used the (implied) 'this' pseudo-pointer in
Java, C++ etc. The wart is in all those languages that don't makes it
mandatory IMHO !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jul 19 '05 #16
bruno modulix <on***@xiludom. gro> wrote:
I've always explicitelly used the (implied) 'this' pseudo-pointer in
Java, C++ etc. The wart is in all those languages that don't makes it
mandatory IMHO !-)


And the correlary wart in Python is that the first argument to a
method is not required to be called "self". The vast majority of
people use "self", but every once in a great while you run into some
yahoo who feels this is the right place to express his creativity and
call it "this", or "obj", or some other obfuscation.
Jul 19 '05 #17
bruno modulix wrote:
Paul McNett wrote:

Sriek wrote:

(snip)

Similarly, why do we have to explicitly use the 'self' keyword
everytime?
This is closer to a wart, IMO,


Here's one of the shorter threads discussing 'self'. I remember one
looooong running thread, but can't seem to find it, at the minute

http://groups.google.co.uk/group/com...c80d8b1c92101c

J
Jul 19 '05 #18
Roy Smith a écrit :
bruno modulix <on***@xiludom. gro> wrote:
I've always explicitelly used the (implied) 'this' pseudo-pointer in
Java, C++ etc. The wart is in all those languages that don't makes it
mandatory IMHO !-)

And the correlary wart in Python is that the first argument to a
method is not required to be called "self".


Well, I would not call this a wart. Because when it's a class method,
naming the fisrt param 'cls' could seem more appropriate !-)
The vast majority of
people use "self", but every once in a great while you run into some
yahoo who feels this is the right place to express his creativity and
call it "this", or "obj", or some other obfuscation.


Then every python programmer in its own mind will throw away this code
with mimics of disgust, and everything is fine.

More seriously, I prefer to have some bozo using this instead of self -
which I can easily correct if needed - than having to deal with implicit
this in a whole code base...
Jul 19 '05 #19
Wow.. Andrew Koenig.. I found your name in many c++ books I read. Never
know you are hanging around in python python mailing-list.
(or perhaps python newsgroup, whatever it is)

Thanks for the explanation.

Andrew Koenig wrote:
"Sakesun Roykiattisak" <sa*****@boonth avorn.com> wrote in message
news:ma******* *************** *************** *@python.org...
Does c++ call base class constructor automatically ??
If I'm not wrong, in c++ you also have to call base class constructor
explicitly.


In C++, if you don't call a base-class constructor (I am saying "a" rather
than "the" because there might be more than one direct base class), it is
called automatically with no arguments. The only case in which you must
call it explicitly is if it will not accept an empty argument list.


Jul 19 '05 #20

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

Similar topics

9
6222
by: Felix Wiemann | last post by:
Sometimes (but not always) the __new__ method of one of my classes returns an *existing* instance of the class. However, when it does that, the __init__ method of the existing instance is called nonetheless, so that the instance is initialized a second time. For example, please consider the following class (a singleton in this case): >>> class C(object): .... instance = None .... def __new__(cls): .... if C.instance is None:
2
1496
by: Steven Bethard | last post by:
Felix Wiemann wrote: > Steven Bethard wrote: >> http://www.python.org/2.2.3/descrintro.html#__new__ > > > I'm just seeing that the web page says: > > | If you return an existing object, the constructor call will still > | call its __init__ method. If you return an object of a different
7
5260
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple inheritance when the base classes are written to expect it, so "Always use super()" seems like bad advice. OTOH sometimes you need super() to get correct behaviour. ISTM "Only use super() when you know you need it" might be
2
1580
by: Thomas Bartkus | last post by:
If I insert an __init__ method in my own class definition, it is incumbent upon me to call the __init__ of any declared ancester to my new class object because my __init__ will override that of any ancester I declare in the header. If I fail to call the ancesters __init__, then it won't happen. The ancester object won't be initialized. But If I *don't* insert my own __init__ in my new class, then any declared ancester __init__ will...
6
1919
by: Rob Cowie | last post by:
Hi all, Is there a simple way to call every method of an object from its __init__()? For example, given the following class, what would I replace the comment line in __init__() with to result in both methods being called? I understand that I could just call each method by name but I'm looking for a mechanism to avoid this.
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...
3
10379
by: Steven W. Orr | last post by:
When I go to create an object I want to be able to decide whether the object is valid or not in __init__, and if not, I want the constructor to return something other than an object, (like maybe None). I seem to be having problems. At the end of __init__ I say (something like) if self.something < minvalue: del self return None and it doesn't work. I first tried just the return None, then I got crafty
3
2677
by: 7stud | last post by:
When I run the following code and call super() in the Base class's __init__ () method, only one Parent's __init__() method is called. class Parent1(object): def __init__(self): print "Parent1 init called." self.x = 10 class Parent2(object):
25
2517
by: Erik Lind | last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more online and can write simple modules, but I still don't get when __init__ needs to be used as opposed to creating a class instance by assignment. For some strange reason the literature seems to take this for granted. I'd appreciate any pointers or links that can help clarify this. Thanks
0
10443
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...
1
10165
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
10002
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
9044
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
7543
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
6783
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();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.