2001 Enchancement Wishlist

Fredrik Lundh fredrik at effbot.org
Fri Dec 29 16:31:29 EST 2000


Raymond Hettinger wrote:
> 3.  __init__ is the only special method that doesn't allow a return value.
>    I think it should continue to return the new object by default but also
>    allow another return item to be substituted.  For example:
>
>             def __init__( self ):
>                    if MyClass.aSingleton == None:
>                          MyClass.aSingleton = self
>                    return MyClass.aSingleton

doesn't make sense: __init__ *initializes* an instance
object, it doesn't create a new one.

:::

if you still think creating new objects just to throw them
away really is a good idea, I'd rather see the return value
check changed from

    value = self.__init__(*args, **kwargs)
    assert value is None

to

    assert value is None or isinstance(value, self.__class__)

or even:

    assert value is None\
        or isinstance(value, types.InstanceType)\
        and value.__class__ is self.__class

:::

allowing an arbitrary value (as long as it's not None) seems
like a really unpythonic idea.

(fwiw, singleton classes are pretty unpythonic too.  python
already has a framework for creating exactly one instance of
something: the module namespace).

</F>





More information about the Python-list mailing list