The order of classes

Daniel Klein danielk at aracnet.com
Mon Mar 19 09:50:15 EST 2001


On 19 Mar 2001 14:06:31 GMT, scarblac at pino.selwerd.nl (Remco Gerlich) wrote:

>Don't use default arguments like this. You do realize, that even if you put
>the definition of foo before bar, that every instance of bar will have 
>*the same foo instance* as its default argument? The 'f = foo()' happens
>only once, namely when the class is defined.
>
>Try something like this:
>
>class bar:
>   def __init__(self, f = None):
>      if f == None:
>         f = foo()
>
>class foo:
>   pass
>   
>
>Now the 'f = foo()' happens when a bar instance is created, and at that
>time, foo already exists.

Excellent answer Remco and it makes a heap of sense! I am already recoding this
as you advise.

Thanks,
Dan



More information about the Python-list mailing list