no return values for __init__ ??

Fred L. Drake, Jr. fdrake at acm.org
Thu Jan 6 13:17:40 EST 2000


Helge Hess writes:
 > recently I was heavily wondering why __init__ does not allow return
 > values ! Is there any special reason not to do this ?

Helge,
  Yes:  It doesn't make sense.  The job of the __init__() method is to 
initialize the instance which has already been created.  There is no
way for a Python program to receive the return value.
  To implement the singleton pattern you appear to be interested in,
try this:

        _C_singleton = None

        class _C:
            ...

        def C():
            global _C_singleton
            if _C_singleton is None:
                _C_singleton = _C()
            return _C_singleton

 >   c1 = C()
 >   c2 = C()
 >   'if c1 is c2' will be true

  This will still be true, and the code will more rationally reflect
what's going on.


  -Fred

--
Fred L. Drake, Jr.	  <fdrake at acm.org>
Corporation for National Research Initiatives




More information about the Python-list mailing list