'wrap inherited classes in Python
Let's say I have a number of inherited classes
class Bird:
def __init__(self):
pass
def identify(self):
print("I am a generic bird!")
class Falcon(Bird):
def __init__(self):
super(Eagle, self).__init__()
def identify(self):
print("I am a Falcon!")
class Eagle(Bird):
def __init__(self):
super(Eagle, self).__init__()
def identify(self):
print("I am an Eagle!")
This allows me to create, e.g., a Falcon using
A = Falcon()
A.identify
I am a Falcon
What I would like to have now is something like a wrapper class, which allows me to choose the kind of bird using a flag. Specifically, I want this behavior
A = BirdSelector(which="Falcon")
A.identify
B = BirdSelector(which="Eagle")
B.identify
I am a Falcon!
I am a Eagle!
In short, A = BirdSelector(which="Eagle")
is supposed to create an instance of Falcons
just as if I had called A = Falcon()
. What is the most pythonic way to do this? Can someone give a hint how to implement BirdSelector
?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|