Skip to content

Instantly share code, notes, and snippets.

@jgrgt
Created March 29, 2011 13:41
Show Gist options
  • Save jgrgt/892376 to your computer and use it in GitHub Desktop.
Save jgrgt/892376 to your computer and use it in GitHub Desktop.
def i_am_wrong(d=dict()):
key = "results"
if key not in d:
d[key] = list()
d[key].append("Hi!")
return d
print "No problems:"
print i_am_wrong(dict())
print i_am_wrong(dict())
print i_am_wrong(dict())
print "But what if we do not pass d?"
print i_am_wrong()
print i_am_wrong()
print i_am_wrong()
% python do_not_use_dict_as_arg.py
No problems:
{'results': ['Hi!']}
{'results': ['Hi!']}
{'results': ['Hi!']}
But what if we do not pass d?
{'results': ['Hi!']}
{'results': ['Hi!', 'Hi!']}
{'results': ['Hi!', 'Hi!', 'Hi!']}
@jgrgt
Copy link
Author

jgrgt commented Mar 29, 2011

Solution:

def i_am_ok(d=None):
    if d is None:
        d = dict()
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment