Shouldn't __init__ return self instead of None? (Was: Re: [Tutor] Why error if method __init__ does not return none)

"Martin v. Löwis" martin at v.loewis.de
Sat Mar 22 17:17:55 EST 2003


Gerrit Holl schrieb:
> This raises an interesting question. Shouldn't it say: __init__ should
> return self...? Because _that_ is what it really does...

That is debatable. If you have

class Foo:
   def __init__(self):
     pass

then this is *really* equivalent to having a function

def Foo(*args):
   result = class("Foo",(),body_of_Foo)
   init = getattr(result, __init__)
   if init: init(*args)
   return result

So the object is returned from calling Foo, not from calling
__init__. It is an initalizer, not a constructor (only Python
2.2 supports constructors). An initializer is a procedure; it
returns nothing.

Regards,
Martin





More information about the Python-list mailing list