Skip to content

Instantly share code, notes, and snippets.

@pydemia
Created June 24, 2022 16:16
Show Gist options
  • Save pydemia/eb5f34e2f350abf331fa40a55e36eb37 to your computer and use it in GitHub Desktop.
Save pydemia/eb5f34e2f350abf331fa40a55e36eb37 to your computer and use it in GitHub Desktop.
Python: force typecasting a class to another
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