Re: return None in constructor call?

Tim Peters (tim@ksr.com)
Fri, 19 Nov 93 17:13:24 EST

> [__init__ returns None, and ...]
> This prevents constructions of the following form:
>
>
> #########
> # this is broken. __init__ returns None
>
> foo = Some_Class(10).some_operation(40)

Have you actually tried this? Here's a class defn:

class Some_Class:
def __init__( self, arg ):
self.arg = arg
def some_operation( self, arg ):
return self.arg + arg

and a session using it:

>>> foo = Some_Class(10).some_operation(40)
>>> foo
50
>>>

Works fine here.

> Is there some reason the Some_Class __init__ routine isn't allowed
> to return 'self' in some form?

Maybe because __init__ is a magical method, and Python "just knows" to
return the self it passed to __init__ to begin with. Agreed it's not
obvious ...

unlike-fermat's-last-theorem-ly y'rs - tim

Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp