Skip to content

Instantly share code, notes, and snippets.

@gjbagrowski
Last active August 29, 2015 13:59
Show Gist options
  • Save gjbagrowski/10836822 to your computer and use it in GitHub Desktop.
Save gjbagrowski/10836822 to your computer and use it in GitHub Desktop.
Nevar forget
# object instances as default arguments are evil
def nevar_forget(constarg={}):
constarg[len(constarg)] = 'the catch'
print(constarg)
return constarg
assert(nevar_forget() == {0: 'the catch'})
assert(nevar_forget() == {0: 'the catch', 1: 'the catch'})
assert(nevar_forget() == {0: 'the catch', 1: 'the catch', 2: 'the catch'})
# class methods are no different
class NevarForget(object):
def __init__(self, thedict={}):
self.thedict = thedict
first = NevarForget()
first.thedict[1] = 'oh yes'
assert(first.thedict == {1: 'oh yes'})
second = NevarForget()
second.thedict[2] = 'oh no'
assert(first.thedict == {1: 'oh yes', 2: 'oh no'})
assert(second.thedict == {1: 'oh yes', 2: 'oh no'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment