Question on default value of __init__
Bernie
bernie at pacific.net.hk
Mon Nov 19 05:57:00 EST 2001
Silly, silly me. Did not get the hint the first time round.
Thanks
Bernie
Martin von Loewis wrote:
>
> 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