diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/mutable_defaults.py	Tue Sep 21 08:30:19 2010 -0700
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+
+class Foo(object):
+  def __init__(self, mutable=['default']):
+    self.foo = mutable
+    self.foo.append(1)
+
+if __name__ == '__main__':
+  bar = Foo()
+  print len(bar.foo)
+  fleem = Foo()
+  print len(fleem.foo)
+  assert len(fleem.foo) == 2, "Don't use default mutable arguments!"