__init__ return value
Alex Martelli
aleax at aleax.it
Fri Sep 26 06:49:39 EDT 2003
Skip Montanaro wrote:
>
> >> No, __init__ method return values are always ignored.
>
> Bob> Oh?
>
> >>>> class A:
> Bob> ... def __init__(self):
> Bob> ... return 'asdf'
> Bob> ...
> >>>> A()
> Bob> Traceback (most recent call last):
> Bob> File "<interactive input>", line 1, in ?
> Bob> TypeError: __init__() should return None
>
> My apologies. It is checked and an exception raised if it's not None.
> That's an implementation detail, not a property of the language.
Actually...:
>>> import this
The Zen of Python, by Tim Peters
[snip]
Errors should never pass silently.
Unless explicitly silenced.
I _would_ call this "a property of the language". Returning not-None
from __init__ is most likely an error: ignoring that error silently by
default would be very un-Pythonic indeed.
As a point of contrast, look at Ruby -- a pretty neat language with a
semantic level and application niche QUITE close to Python's, but most
definitely NOT having "errors should never pass silently" as a
property of the language. Most fundamental errors, say indexing an
array with an index outside of the array's bounds, DO pass silently in
Ruby -- returning nil (like Python's None) for indexing-reads out of
bounds, silently and implicitly padding the array with nil's for
indexing-writes out of bounds. I think this language property and its
practical implications make an ENORMOUS difference to the approach
one takes when programming in the two different languages...
Alex
More information about the Python-list
mailing list