Last active
January 4, 2018 05:24
-
-
Save charlesreid1/73cf5b46c5cb1d415f47b9fa84936fa8 to your computer and use it in GitHub Desktop.
Give a Python class a brain transplant by modifying __class___.
This file contains hidden or 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
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