Skip to content

Instantly share code, notes, and snippets.

@kiyohara
Created January 4, 2015 12:59
Show Gist options
  • Save kiyohara/836bb3d482b807f91cee to your computer and use it in GitHub Desktop.
Save kiyohara/836bb3d482b807f91cee to your computer and use it in GitHub Desktop.
default arg trap
class X:
def __init__(self, x={}):
self.var = x
def set(self, val):
self.var['key'] = val
class Y(X):
def __init__(self,x=None):
self.var = x if x is not None else {}
if __name__ == '__main__':
x1 = X()
x1.set('test')
print x1.var # {'key': 'test'}
x2 = X()
print x2.var # {'key': 'test'}
y1 = Y()
y1.set('test')
print y1.var # {'key': 'test'}
y2 = Y()
print y2.var # {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment