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

Gonçalo Rodrigues op73418@mail.telepac.pt
Sun Mar 23 09:21:07 2003


> [FUP: python-list@python.org]
>
> Bob Gailer schreef op zaterdag 22 maart om 18:05:29 +0000:
> > What is "returned" here is an instance of the class, not what is
returned
> > by the __init__ method. I guess that "TypeError: __init__() should
return
> > None" is a way of telling you not to return something else with the
> > expectation that it will be available.
>
> This raises an interesting question. Shouldn't it say: __init__ should
> return self...? Because _that_ is what it really does...
>
> Of course, changing this would break code, but moving towards this might
> be a good idea...?
>

No. __init__ takes the name from initializer: it *initializes* the state of
the instance - the instance already exists as a perfectly valid Python
object. Returning a *new* instance is for the __new__ magic method (Python
>= 2.2) where allocation and such stuff can be taken care of.

> yours,
> Gerrit.

With my best regards,
G. Rodrigues