Skip to content

Instantly share code, notes, and snippets.

@predakanga
Created January 15, 2013 12:36
Show Gist options
  • Save predakanga/4538338 to your computer and use it in GitHub Desktop.
Save predakanga/4538338 to your computer and use it in GitHub Desktop.
class A(object):
def id(self):
return "Hello"
class B(object):
def id(self):
return "Hi"
dict1 = {}
dict2 = {}
storedVar = A()
print "Unstored: %s" % (storedVar.id(),)
dict1['foo'] = storedVar
dict2['bar'] = storedVar
storedVar = None
print "Dict 1: %s" % (dict1['foo'].id(),)
# Prints "Dict 1: Hello"
print "Dict 2: %s" % (dict2['bar'].id(),)
# Prints "Dict 2: Hello"
print "Changing class"
dict1['foo'].__class__ = B
print "Dict 1: %s" % (dict1['foo'].id(),)
# Prints "Dict 1: Hi"
print "Dict 2: %s" % (dict2['bar'].id(),)
# Prints "Dict 2: Hi"
print "Magic!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment