Last active
August 29, 2015 13:59
-
-
Save gjbagrowski/10836822 to your computer and use it in GitHub Desktop.
Nevar forget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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