我正在寻找从注册为ABC的子类的类的实例调用抽象基类'方法的正确方法。这是一些非常基本的测试代码,首先要弄清楚如何使这项工作。这是我目前所在的地方:
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)
虽然这种方法似乎过度涉及改变两种Dog方法,但我希望能够灵活地将行为分配给未知数量的实例。我希望能够在不知道实际行为的情况下调用speak()和move()。我也喜欢这种方法,因为我能够轻松删除或更改类的注册行为,而无需更改任何现有代码。
代码当前读取的方式nora.speak()和nora.move()调用从Dog到Beagle的继承方法,它只包含pass。
我很感激,如果有人对我在这一点上需要做什么有所了解,以使注册行为的方法可以调用,或者我的方法完全有缺陷。
答案 0 :(得分:0)
这是我的尝试(可能不是最狡猾的方式,但接近你的帖子):
class Animals(object):
def speak(self):
return self.speak_action
def swim(self):
return self.swim_action
def move(self):
return self.move_action
class Dog(Animals):
@property
def speak_action(self):
return "bark bark bark"
@property
def move_action(self):
return "I'm Running"
class Beagle(Dog):
@property
def speak_action(self):
return "ahwoooooo"
class Duck(Animals):
@property
def swim_action(self):
return "Im floating"
@property
def speak_action(self):
return "Quack!!"
@property
def move_action(self):
return "I Fly!"
class Mallard(Duck):
@property
def speak_action(self):
return "I'm Flying higher"
(最好让异常冒泡)
In [825]: d = Dog()
In [826]: d.speak()
Out[826]: 'bark bark bark'
In [827]: d.move()
Out[827]: "I'm Running"
In [828]: d.swim()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-828-c6d2ef2b464d> in <module>()
----> 1 d.swim()
.stuff/python/git_py/help_so.py in swim(self)
4
5 def swim(self):
----> 6 return self.swim_action
7
8 def move(self):
AttributeError: 'Dog' object has no attribute 'swim_action'
您可以选择要委派的内容:
In [830]: b = Beagle()
In [831]: b.speak()
Out[831]: 'ahwoooooo'
In [832]: b.move()
Out[832]: "I'm Running"
In [833]: b.swim()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-833-9c7b1a0c0dca> in <module>()
----> 1 b.swim()
/stuff/python/git_py/help_so.py in swim(self)
4
5 def swim(self):
----> 6 return self.swim_action
7
8 def move(self):
AttributeError: 'Beagle' object has no attribute 'swim_action'
创造具有更多技能的其他动物:
In [849]: dd = Duck()
In [850]: dd.speak()
Out[850]: 'Quack!!'
In [851]: dd.move()
Out[851]: 'I Fly!'
In [852]: dd.swim()
Out[852]: 'Im floating'
您可以将特定内容,甚至默认值,特定内容发送到主类,并且可以根据需要进行扩展。