I am searching for the proper way to call an abstract base class' method from an instance of a class that is registered as a subclass of the ABC. This is some very basic test code to first figure out how to make this work. This is where I am currently at:
from abc import ABCMeta
# Dog class
class Dog(object):
def speak(self):
pass
def move(self):
pass
# Barking behaviors
class Bark(object):
__metaclass__ = ABCMeta
def speak(self):
print "bark bark bark"
class Howl(object):
__metaclass__ = ABCMeta
def speak(self):
print "ahwoooooo"
# Movement behaviors
class Run(object):
__metaclass__ = ABCMeta
def move(self):
print "I'm running"
class Walk(object):
__metaclass__ = ABCMeta
def move(self):
print "I'm walking"
# Dog implementations
class Beagle(Dog):
pass
Howl.register(Beagle)
Run.register(Beagle)
nora = Beagle()
nora.speak() # THIS IS THE ISSUE: Calls speak() from original Dog class
nora.move() # Need to call move() from registered ABC
# Test to be sure .register() was used properly
assert isinstance(nora, Howl)
While this approach may seem overly involved to alter the two Dog methods, I am looking to have the flexibility of being able to assign the behaviors to an unknown amount of instances. I'd like to be able to call speak() and move() without the instance knowing the actual behavior. I also like this approach because I am able to easily remove or change the behavior that a class is registered too, without altering any existing code.
The way the code reads currently nora.speak() and nora.move() call the inherited methods from Dog to Beagle, which just contain pass.
I'd appreciate if anyone has any insight on what I need to do from this point to make the registered behavior's methods callable, or if my approach is flawed entirely.