__init__ explanation please

Reedick, Andrew jr9445 at ATT.COM
Mon Jan 14 12:08:02 EST 2008


> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Hrvoje Niksic
> Sent: Monday, January 14, 2008 11:29 AM
> To: python-list at python.org
> Subject: Re: __init__ explanation please
> 
> Mel <mwilson at the-wire.com> writes:
> 
> >> I don't understand the purpose of this "correction".  After all,
> >> __init__ *is* the closest equivalent to what other languages would
> >> call a constructor.
> >
> > Nevertheless, __init__ doesn't construct anything.
> 
> Only if by "construct" you mean "allocate".  __init__ starts out with
> an empty object and brings it to a valid state, therefore
> "constructing" the object you end up with.  That operation is exactly
> what other languages call a constructor.


Nah.  Most other languages combine the constructor and an init function.
Normally with c++ I'll have the constructor call an Init() function so I
can re-initialize the object as needed.  Python has explicitly split the
two.


Besides, the Python docs say that __new__ is the constructor and
__init__ may or may not be called after the instance is created:

__new__( cls[, ...]) 

	Called to create a new instance of class cls. __new__() is a
static method (special-cased so you need not declare it as such) that
takes the class of which an instance was requested as its first
argument. The remaining arguments are those passed to the object
constructor expression (the call to the class). The return value of
__new__() should be the new object instance (usually an instance of
cls).

	...
	If __new__() returns an instance of cls, then the new instance's
__init__() method will be invoked
	...
	If __new__() does not return an instance of cls, then the new
instance's __init__() method will not be invoked. 


__init__( self[, ...]) 

	Called when the instance is created.
	...
	As a special constraint on constructors, no value may be
returned;



Also, how can a constructor require 'self' as an argument...?
__init__(self, ...)


If the __init__ function is called by the constructor it cannot return a
value.  However if called as a normal function, it can return a value.
__init__ is just a function that gets called by the constructor, which
is __new__.


count = 0
class AClass (object):
	def __init__ (self):
		self.a = 4

		global count
		if count > 0:
			return 'hello world'

		count += 1


a = AClass()

print a.a
print a.__init__()


c:\foo\a.py>
4
hello world



*****

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. GA622





More information about the Python-list mailing list