Question on default value of __init__

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Nov 19 05:38:16 EST 2001


Bernie <bernie at pacific.net.hk> writes:

> When I run test.py, an instance of foo is created.  Is this the
> default behavior in Python for default value?

Yes, default values are created when the function is defined. This is
the way Python works; it is not just the default.

> Are there any way to delay the creation of foo() until bar() is
> created.

>         def __init__( self, param=foo()):
>             assert isinstance( param, foo), '"in" must be an instance of foo'
>             pass

You should write it like this

  def __init__(self, param = None):
    if param is None:
       param = foo()
    else:
       assert isinstance(param, foo), '"in" must be an instance of foo'

HTH,
Martin



More information about the Python-list mailing list