Question on default value of __init__

Bernie bernie at pacific.net.hk
Mon Nov 19 05:42:29 EST 2001


Thanks for the confirmation, Werner.  I am intented to use the null object 
pattern, that is the reason why I use foo() as default.

Bernie

Werner Schiendl wrote:
> 
> Hi,
> 
> yes, this is the default behaviour of Python.
> The default value you provide is stored and passed to every invokation of
> the function/method.
> This means, the _same_ instance of foo will be passed for every call to
> bar.__init__()
> 
> To get what you probably want, use None as the default and handle that
> special case by creating a new instance explicitely.
> This also prevents an instance of foo being created, before you create bar.
> 
> hth
> Werner
> 
> "Bernie" <bernie at pacific.net.hk> wrote in message
> news:3BF8D3BC.105F0536 at pacific.net.hk...
> > Hello all,
> >
> > When I run test.py, an instance of foo is created.  Is this the default
> behavior
> > in Python for default value?  i.e. The value must be exist as an object.
> Are
> > there any way to delay the creation of foo() until bar() is created.
> >
> > Bernie
> >
> > test.py:
> >
> >     class foo:
> >         def __init__( self, param=None):
> >             print 'executed!'
> >     self.__param = param
> >
> >         def __nonzero( self):
> >             if self.__param:
> >                 return 1
> >             else:
> >                 return 0
> >
> >     class bar:
> >         def __init__( self, param=foo()):
> >             assert isinstance( param, foo), '"in" must be an instance of
> foo'
> >             pass



More information about the Python-list mailing list