Skip to content

Instantly share code, notes, and snippets.

@saghul
Created June 5, 2011 11:11
Show Gist options
  • Save saghul/1008874 to your computer and use it in GitHub Desktop.
Save saghul/1008874 to your computer and use it in GitHub Desktop.
Default function arguments (mutable type)
In [1]: class Foo(object):
...: def __init__(self, bar=[]):
...: self.bar = bar
...:
...:
In [2]: f1 = Foo()
In [3]: f2 = Foo()
In [4]: f1.bar
Out[4]: []
In [5]: f2.bar
Out[5]: []
In [6]: f1.bar.append(1)
In [7]: f1.bar
Out[7]: [1]
In [8]: f2.bar
Out[8]: [1]
In [9]: id(f1.bar)
Out[9]: 39526344
In [10]: id(f2.bar)
Out[10]: 39526344
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment