comparison python/example/mutable_defaults.py @ 717:d6f659169b49

mutable defaults -> example
author Jeff Hammel <k0scist@gmail.com>
date Thu, 23 Oct 2014 15:29:48 -0700
parents python/mutable_defaults.py@434c65593612
children
comparison
equal deleted inserted replaced
716:ca1f21d9e621 717:d6f659169b49
1 #!/usr/bin/env python
2
3 # see also: http://www.daniweb.com/software-development/python/threads/66697
4
5 class Foo(object):
6 def __init__(self, mutable=['default']):
7 self.foo = mutable
8 self.foo.append(1)
9
10 if __name__ == '__main__':
11 bar = Foo()
12 print len(bar.foo)
13 fleem = Foo()
14 print len(fleem.foo)
15 assert len(fleem.foo) == 2, "Don't use default mutable arguments!"