Mercurial > hg > config
annotate python/example/mutable_defaults.py @ 767:35f8751c0930
it is very annoying to have ones overrides overridden; see also http://stackoverflow.com/questions/25381304/why-type-cd-on-mac-os-states-that-cd-is-a-function
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 28 Jan 2016 14:02:17 -0800 |
parents | d6f659169b49 |
children |
rev | line source |
---|---|
91
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
184 | 3 # see also: http://www.daniweb.com/software-development/python/threads/66697 |
4 | |
91
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 class Foo(object): |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 def __init__(self, mutable=['default']): |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 self.foo = mutable |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 self.foo.append(1) |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 if __name__ == '__main__': |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 bar = Foo() |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 print len(bar.foo) |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 fleem = Foo() |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 print len(fleem.foo) |
70544c7406e2
add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 assert len(fleem.foo) == 2, "Don't use default mutable arguments!" |