comparison python/mutable_defaults.py @ 91:70544c7406e2

add illustration program for mutable arguments, since this comes up a bunch
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 21 Sep 2010 08:30:19 -0700
parents
children 434c65593612
comparison
equal deleted inserted replaced
90:6d09c2a8e5e9 91:70544c7406e2
1 #!/usr/bin/env python
2
3 class Foo(object):
4 def __init__(self, mutable=['default']):
5 self.foo = mutable
6 self.foo.append(1)
7
8 if __name__ == '__main__':
9 bar = Foo()
10 print len(bar.foo)
11 fleem = Foo()
12 print len(fleem.foo)
13 assert len(fleem.foo) == 2, "Don't use default mutable arguments!"