Newbie asks about static variables...

Gordon McMillan gmcm at hypernet.com
Thu May 6 14:47:10 EDT 1999


Emile writes:
 
> Thanks for your reply.  I haven't yet done any specific testing, but
> would you know if in fact the complaints of this behavior result
> from misunderstanding, or is there an actual bug such that the
> results are unpredictable?

All user-land bugs - they write
 def foo(lst=[]):

where they should have written
 def foo(lstarg=None):
    lst = lstarg or [] # or some variation

> In other words, is there any reason to avoid using this construct
> knowing that this is how it works?  

Just to avoid confusing those who aren't aware of how it works.

> Or, is there reason to think
> that it may be changed to behave differently in some future release?

Not in Python 1.6. Python 2 will allow breaking backwards 
compatibility, so a year or so from now, this might come up for 
discussion.

> 
> > Emile van Sebille, in reply to Evan's creative use of a mutable
> > default:
> >
> > > Being also new at Python, doesn't your example follow the example
> > > from section 4.7 of the tutorial?
> > >
> > > Why is this *evil*?  It's right there in the tutorial.  </aside - I
> > > really do believe everything I read> What is Guido not saying? What
> > > are the dangers?  Where is the case study?
> >
> > The case study is the volume of posts complaining that something's
> > broken. This is in the top 3. Evan's use of it as a "feature" is
> > outnumbered 1,000 to 1 by complaints that it's a bug.
> >
> > Congratulations - you may well be unique in having both read and
> > retained this from the tutorial!
> >
> > - Gordon


- Gordon




More information about the Python-list mailing list