Skip to content

Instantly share code, notes, and snippets.

@mirekfranc
Last active August 29, 2015 14:20
Show Gist options
  • Save mirekfranc/757c20e71813bef7fe56 to your computer and use it in GitHub Desktop.
Save mirekfranc/757c20e71813bef7fe56 to your computer and use it in GitHub Desktop.
default arguments evaluated once at function definition...
>>> def a(x=False, y=[]):
... if not x:
... y.append (5)
... else:
... print y
...
>>> a()
>>> a()
>>> a()
>>> a()
>>> a()
>>> a(True)
[5, 5, 5, 5, 5]
>>> a()
>>> a(True)
[5, 5, 5, 5, 5, 5]
>>>
>>> def b(x=False, y=None):
... if y is None:
... y=[]
... if not x:
... y.append (5)
... else:
... print y
...
>>> b()
>>> b()
>>> b()
>>> b(True)
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment