Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Last active January 4, 2018 05:24
Show Gist options
  • Save charlesreid1/73cf5b46c5cb1d415f47b9fa84936fa8 to your computer and use it in GitHub Desktop.
Save charlesreid1/73cf5b46c5cb1d415f47b9fa84936fa8 to your computer and use it in GitHub Desktop.
Give a Python class a brain transplant by modifying __class___.
class Foo(object):
def whoami(self):
self.foowuz = 666
print("I'm Foo, foo!")
class Bar(object):
def whoami(self):
self.barwuz = 777
print("I'm Bar, sucka!")
obj = Foo()
obj.whoami()
# prints I'm Foo, foo!
# foowuz = 666
# no attribute barwuz
obj.__class__ = Bar
obj.whoami()
# prints I'm Bar, sucka!
# foowuz = 666
# barwuz = 777
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment