Created
June 24, 2022 16:16
-
-
Save pydemia/eb5f34e2f350abf331fa40a55e36eb37 to your computer and use it in GitHub Desktop.
Python: force typecasting a class to another
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
class A: | |
def __init__(self, name): | |
self.name = name | |
def execute(self): | |
raise NotImplementedError() | |
class B: | |
def __init__(self, name): | |
self.name = name | |
def execute(self): | |
print(self.name) | |
inst = A("this") | |
inst.execute() # NotImplementedError | |
inst.__class__ = B | |
inst.execute() # this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment