no return values for __init__ ??

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Jan 6 14:55:16 EST 2000


Helge Hess <helge.hess at mdlink.de> writes:

> In the current interpreter not. That is what annoys me, since it is an
> completly articial limitation I don't understand.
> As mentioned it took only 5 minutes to 'correct' this limitation.

I assume you also took care of decrefing the 'garbage' object as well,
right?

Anyway, if your change is accepted into the language, you get a
serious incompatibility:

class A:
  def __init__(self, list = None):
    self.value = 0
    if list is None:
      return
    for item in list:
      self.value = self.value + item

Today, A() would give you a new object. With your change, A() gives
you None (because the return is implicitly 'return None').

What's worse, if you have

class A:
  def __init__(self):
    print "Here"

means that you cannot create instances of A at all, as A() will return
with an implicit 'return None'. That could be fixed by telling the
compiler to put 'return self' into the constructors by default, but
that juse makes __init__ different from the other functions, again.

This should explain why we can't change the language like that. That
does not explain why the language is the way it is: I guess that
__init__ was designed not really as a constructor (it does not create
the object), but as an initializer - just like C++ or Java, where you
can't return something different from the "constructor", either.

Regards,
Martin




More information about the Python-list mailing list