annotate python/mutable_defaults.py @ 115:c499f5a598cf

make variable interpolation sorta work; the nonce thing probably isnt the best idea
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 02 Dec 2010 17:16:53 -0800
parents 70544c7406e2
children 434c65593612
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
70544c7406e2 add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 class Foo(object):
70544c7406e2 add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 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
5 self.foo = mutable
70544c7406e2 add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 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
7
70544c7406e2 add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 if __name__ == '__main__':
70544c7406e2 add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 bar = Foo()
70544c7406e2 add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 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
11 fleem = 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(fleem.foo)
70544c7406e2 add illustration program for mutable arguments, since this comes up a bunch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 assert len(fleem.foo) == 2, "Don't use default mutable arguments!"