confused with local variables being retained
Dan Brown
danb at alum.mit.edu
Tue Nov 21 13:48:59 EST 2000
Hi there,
I'm being confused (I guess) by namespace or scoping rules in Python; I
don't quite know why this piece of code is having the effect that it is:
class simple:
def __init__ (self, label = 'spam'):
self.label = label;
def display (self, stufflist = []):
stufflist.append (self.label)
print stufflist
When I run the "display" method, this happens:
>>> B = simple ('B')
>>> B.display()
['B']
>>> B.display()
['B', 'B']
>>> B.display()
['B', 'B', 'B']
Why is the stufflist variable staying active from iteration to
iteration? When I use non-lists, it seems to go away.
Thanks much,
-dan brown
danb at alum.mit.edu
More information about the Python-list
mailing list