__init__ explanation please
Mel
mwilson at the-wire.com
Mon Jan 14 11:20:18 EST 2008
Hrvoje Niksic wrote:
> Wildemar Wildenburger <lasses_weil at klapptsowieso.net> writes:
>
>> Jeroen Ruigrok van der Werven wrote:
>>> To restate it more correctly: __init__ is akin to a constructor.
>>>
>> No. See Hrvoje Niksic's reply (and Ben Finney's to which it was a
>> reply).
>>
>> __init__() /initializes/ an instance (automatically after
>> creation). It is called, /after/ the instance has been constructed
>
> 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. You can even call
it to reinitialize an existing object:
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class AClass (object):
... def __init__ (self):
... self.a = 4
...
>>> a = AClass()
>>> a.a
4
>>> a.a = 5
>>> a.a
5
>>> a.__init__()
>>> a.a
4
Cheers, Mel.
More information about the Python-list
mailing list