__init__ explanation please

S

Steven D'Aprano

What one is "in reality" calling is the '__new__' method of the Person
class. That function, in turn, is creating a new Person instance, and
calling the '__init__' method of the newly-created instance. Finally,
the '__new__' method returns that instance back to the caller.


But not if Person is an old-style class.
 
B

Ben Finney

Hrvoje Niksic said:
I don't understand the purpose of this "correction". After all,
__init__ *is* the closest equivalent to what other languages would
call a constructor.

No. That would be '__new__', which actually constructs the instance,
and actually returns it to the caller. '__init__' does neither of
those.

It so happens that, in Python, one usually overrrides the initialiser
and not the constructor. Thus, the confusion is understandable, but
still regrettable and avoidable.
 
H

Hrvoje Niksic

Ben Finney said:
No. That would be '__new__', which actually constructs the instance,

That's not what other OO languages (C++, Java) actually call a
constructor, so your correction is misplaced. My other posts in this
thread have expanded on this.
 
S

Steven D'Aprano

That's not what other OO languages (C++, Java) actually call a
constructor, so your correction is misplaced. My other posts in this
thread have expanded on this.


How fortunate that Python isn't one of those other OO languages,
otherwise it might cause a bit of confusion.
 
B

Ben Finney

Hrvoje Niksic said:
That's not what other OO languages (C++, Java) actually call a
constructor

More fool them, then. It seems that the best referent for the term
"constructor" is the thing that does the constructing and returns the
result.
 
B

Bruno Desthuilliers

Hrvoje Niksic a écrit :
That's not what other OO languages (C++, Java) actually call a
constructor,

There are actually quite a few other OOPLs than C++ and Java, and at
least one of them (namely Smalltalk, which predates both C++ and Java)
uses distinct phases for allocation and initialisation.

IOW, it's not because C++ and/or Java use a given terminology that this
terminology should be blindly applied to each and every other OOPL.
FWIW, there are quite a lot of other differences between C++/Java and
Python when it comes to object model, and what OO is is definitively
*not* defined by C++ and/or Java.

So while it's true that __init__ is the closest equivalent to what C++
and Java (and possibly a couple "other languages") call a constructor,
it doesn't imply that you should refer to it as "the constructor". As
Neil Cerutti points out, there's in fact nothing like a 'constructor
method' in Python : there are a "__new__" method, an "__init__" method,
and "constructor expressions" which may invoke them !-)
 
H

Hrvoje Niksic

So while it's true that __init__ is the closest equivalent to what
C++ and Java (and possibly a couple "other languages") call a
constructor, it doesn't imply that you should refer to it as "the
constructor". As Neil Cerutti points out, there's in fact nothing
like a 'constructor method' in Python : there are a "__new__"
method, an "__init__" method, and "constructor expressions" which
may invoke them !-)

I agree with this. The poster I was responding to called __init__
"akin to a constructor", which (to me) implied connection to other
languages, not aspiration to define __init__ as THE constructor.
 
L

Lie

I've been in this Python mailing list for a few days, and I've noticed
several things here: There are too many fundamentalist!

Don't play stupid and all, don't be a fundamentalist. It might be true
that __init__ isn't a constructor and __new__ might be the constructor
(some people even claimed __new__ is also not a constructor).

From the base definition of a constructor: constructor is the creator
of an object. In this case, __new__ is technically the constructor
while __init__ is an initializer.

However, it is also to be noted that __init__ is what makes an object
meaningful, and that makes it a constructor in a sense (while still
technically a constructor). Without initialization, an object is
meaningless, even if the definition of the initializer is to leave it
as it is.

Python creates object by doing something like this:
a = anObject(arg1, arg2, arg3)

These arguments is then passed to __new__ and __init__ for their
arguments in its sake of creating and initializing the object. Then
anObject() returns an instance of anObject.

From an outsider's point of view, there is no difference between
__new__ and __init__ since they're "implementation details" (in other
languages, these are private functions[1] that is invisible to
outsiders, Python doesn't like privacy but the semantic of being
implementation detail still exist). For an outsider, there is
absolutely no need to know that __new__ and __init__ exists, they just
need to know anObject()'s arguments, which is the public view of the
constructor and initializer[2].

[1] Well, for fundamentalists: constructors aren't usually private
though, usually they're Friend or Protected Friend which prohibits
outsiders from calling it but allow other classes inheriting from it
to call them.
[2] In this sense, from outsider's POV anObject() is the constructor.


If you can't be convinced with this argument, then I'd give you
another that's a bit more Pythonic:
DUCK TYPING: If it looks like a duck, walks like a duck, and quacks
like a duck, it is a duck!

From the class programmer's point of view, __init__ acts like an
object constructor in other languages, there is no significant
difference between __init__ and constructor in other languages. The
fact that __init__ works with side-effect as opposed to returning the
object is not a significant point and can be considered as an
implementation difference (I'm not aware of any major programming
language that returns an instance of itself in its return value
[except for Python]).

For example, in VB.NET, there is no doubt that Sub New() is a
constructor, despite New() works only by side effect, and returning
anything results in an error (since it is a Sub or a method in
Python's dictionary). Not only VB, C++ and C# also use side effect in
its constructors and doesn't return a value. In this sense, VB's New, C
++ constructor, and C# constructor is equal to Python's __init__, thus
the Duck Typing spirit applies here.
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of Lie
Sent: Tuesday, January 15, 2008 5:03 PM
To: (e-mail address removed)
Subject: Re: __init__ explanation please

I've been in this Python mailing list for a few days, and I've noticed
several things here: There are too many fundamentalist!

Don't play stupid and all, don't be a fundamentalist. It might be true
that __init__ isn't a constructor and __new__ might be the constructor
(some people even claimed __new__ is also not a constructor).


Purist is a better term. Fundamentalist is three syllables closer to
Holy War.

of an object. In this case, __new__ is technically the constructor
while __init__ is an initializer.

However, it is also to be noted that __init__ is what makes an object
meaningful, and that makes it a constructor in a sense (while still
technically a constructor). Without initialization, an object is
meaningless, even if the definition of the initializer is to leave it
as it is.


You don't need to have an __init__ defined. A subclass has to
explicitly call the parent's __init__ or the parent's __init__ is never
run. If the __init__ makes the object meaningful, then how meaningful
is an object without an __init__? I'm pretty sure that an object
without an __init__ is still a viable, working object.

If you can't be convinced with this argument, then I'd give you
another that's a bit more Pythonic:
DUCK TYPING: If it looks like a duck, walks like a duck, and quacks
like a duck, it is a duck!


But you don't need __init__ to be a duck!

object constructor in other languages, there is no significant
difference between __init__ and constructor in other languages.


How many times can you call an object's constructor in other languages?
__init__ can be called repeatedly.


__init__ is the last straw that breaks the camel's back. Or rather, the
last method we see in the object creation process, and thus must be
'guilty' of being a constructor. Only a fundamentalist would blame the
victim instead of the real criminal, __new__.


We're splitting hairs. And I'm pretty sure that, aside from being a
spiffy thought experiment, no one cares as long as it works and makes
sense. =)


Repeated for clarity: smiley --> =) <-- smiley



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625
 
S

Steven D'Aprano

I've been in this Python mailing list for a few days, and I've noticed
several things here: There are too many fundamentalist!

Don't play stupid and all, don't be a fundamentalist. It might be true
that __init__ isn't a constructor and __new__ might be the constructor

It is true.

(some people even claimed __new__ is also not a constructor).

They did? I must have missed them.

From the base definition of a constructor: constructor is the creator of
an object. In this case, __new__ is technically the constructor while
__init__ is an initializer.

Yes, that is correct, although I too have been known to use "constructor"
to *informally* refer to __init__. It's a bad habit, and while
technically wrong, not always unforgivably wrong.

However, it is also to be noted that __init__ is what makes an object
meaningful, and that makes it a constructor in a sense (while still
technically a constructor). Without initialization, an object is
meaningless, even if the definition of the initializer is to leave it as
it is.

Nope, not at all. The following class does not call the initializer:

class MyClass(object):
class __metaclass__(type):
def __call__(cls, *args, **kwargs):
obj = cls.__new__(cls)
print "There is no __init__."
return obj
def __new__(cls, *args, **kwargs):
print "This is the constructor, see me construct an instance!"
return object.__new__(cls)
def __init__(self, *args, **kwargs):
raise Exception("die die die!!!")


Now use it:
This is the constructor, see me construct an instance!
There is no __init__.

And call the initializer by hand:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 7, in __init__
Exception: die die die!!!


Here's a class with no constructor (or rather, a constructor that the
user *can't get to*):

class OldClass:
def __new__(cls, *args, **kwargs): # this is not called
raise Exception("die die die!!!")
def __init__(self, *args, **kwargs):
print "This is the initializer, see me initialize " \
"the already-constructed instance 'self'!"

This is the initializer, see me initialize the already-constructed
instance 'self'!


For various reasons, Python splits the process of constructing and
initializing instances into two stages. What other languages do is
irrelevant. Perhaps Java and C++ don't need to distinguish between
"constructor" and "initializer", but Python does.


Python creates object by doing something like this: a = anObject(arg1,
arg2, arg3)

That's what the programmer does. Under the hood, Python does something
different.

These arguments is then passed to __new__ and __init__ for their
arguments in its sake of creating and initializing the object. Then
anObject() returns an instance of anObject.

Assuming the standard metaclass.

From an outsider's point of view, there is no difference between __new__
and __init__ since they're "implementation details"

No, they most certainly are not implementation details. ANY
implementation of Python MUST use __new__ and __init__ or else it is not
Python, it is a different language. The nature of how Python creates
instances is part of the language specification, not the implementation.

(in other languages,
these are private functions[1] that is invisible to outsiders, Python
doesn't like privacy but the semantic of being implementation detail
still exist). For an outsider, there is absolutely no need to know that
__new__ and __init__ exists, they just need to know anObject()'s
arguments, which is the public view of the constructor and
initializer[2].

I don't understand your argument. If you are saying that people who don't
care about the details of Python instance creation don't care about the
details of Python instance creation, then you're right, but it's a rather
pointless observation. Yes, people who don't care don't care.

But people who want to:

(1) Program successfully in Python;

(2) Compare how Python works to other computer languages;

(3) Do metaclass programming; or

(4) Find out how Python creates instances

will care about the details. Anybody asking for an explanation of
__init__ (like this thread!) is asking about the details. Why on earth do
you think it is a bad thing to answer the question accurately?


[snip]
If you can't be convinced with this argument, then I'd give you another
that's a bit more Pythonic:
DUCK TYPING: If it looks like a duck, walks like a duck, and quacks like
a duck, it is a duck!

From the class programmer's point of view, __init__ acts like an object
constructor in other languages, there is no significant difference
between __init__ and constructor in other languages.

Fortunately, Python isn't those other languages. We're not discussing how
Java creates instances, or C++, or VisualBasic. We're discussing Python,
so any answer given that starts off "Well, in Java it works like this..."
is almost certainly going to be useless to the Python programmer asking
about Python.

[snip]
In this sense, VB's New, C ++
constructor, and C# constructor is equal to Python's __init__, thus the
Duck Typing spirit applies here.

It isn't enough to quack like a duck. It also needs to walk like a duck
and swim like a duck too.
 
L

Lie

You don't need to have an __init__ defined.  A subclass has to
explicitly call the parent's __init__ or the parent's __init__ is never
run.  

In other languages, constructor might be optional. In the case of non-
existent constructor, compilers would add an empty constructor, but
what's the difference (from programmer's POV) between Python ignoring
__init__ and other languages adding empty constructor? That's just an
implementation detail.
If the __init__ makes the object meaningful, then how meaningful
is an object without an __init__?  

It actually depends on the object, some objects might be pretty
meaningless without being initialized (or its members altered from
outside very carefully). Examples include a simple vector class. If
the vector is not initialized, the x and y equals None, which is not a
valid value for vector, which means the object is meaningless.
I'm pretty sure that an object
without an __init__ is still a viable, working object.

I'm sure it is, although it's initial value might not be a valid
value.
But you don't need __init__ to be a duck!
lol...


How many times can you call an object's constructor in other languages?
__init__ can be called repeatedly.

That's a very good argument to separate __init__ from a real
constructor, but how many projects you do would require object
recycling (which is the only reason I can think of for calling
initializers more than once)? Object recycling should only be done on
systems which lacks resources because it have big potential to
introduce bugs caused by incomplete cleaning.
__init__ is the last straw that breaks the camel's back.  Or rather, the
last method we see in the object creation process, and thus must be
'guilty' of being a constructor.  Only a fundamentalist would blame the
victim instead of the real criminal, __new__.

It's not about blaming, rather they shared parts in the act.
We're splitting hairs.  And I'm pretty sure that, aside from being a
spiffy thought experiment, no one cares as long as it works and makes
sense.   =)

I agree with that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top