What I have in an external library:
# external package content
class Foo:
def func(): ...
class Bar(Foo):
def func():
super().func()
What I need in my code:
from external_library import Bar
class MyOwnCustomFoo:
def func(): ...
# Do some magic here to replace a parent class with another class without keeping the old parent
class Bar(MyOwnCustomFoo):
def func():
super().func()
Goals I need to achieve:
- I must have all
Bar
methods without copy/pasting them. Bar
must inherit from my own class, not from another one.
CodePudding user response:
You could assign the methods from the external library class to your own class, mimicking inheritance (AFAIK this is called monkey-patching):
from external_library import Bar as _Bar
class Bar(MyOwnCustomFoo):
func = _Bar.func
...
CodePudding user response:
You need to use what's called a "monkey patch" to override the bar.foo()
in the imported instance of the class. This link has a sufficient example:
See Monkey Patching in Python (Dynamic Behavior) - GeeksforGeeks