python:多重继承和基类中的 __add__()
我有一个基类,我想在其中处理 __add__()
并希望在 __add__
ing 两个子类实例时支持 - 也就是说,两个子类的方法都在结果实例。
import copy
class Base(dict):
def __init__(self, **data):
self.update(data)
def __add__(self, other):
result = copy.deepcopy(self)
result.update(other)
# how do I now join the methods?
return result
class A(Base):
def a(self):
print "test a"
class B(Base):
def b(self):
print "test b"
if __name__ == '__main__':
a = A(a=1, b=2)
b = B(c=1)
c = a + b
c.b() # should work
c.a() # should work
编辑:更具体地说:我有一个类Hosts
,它包含一个dict(host01=.., host02=..)
(因此是 dict
的子类) - 这提供了一些基本方法,例如 run_ssh_commmand_on_all_hosts()
现在我有一个子类 HostsLoadbalancer
,它包含一些特殊方法,例如 drain()
,并且我有一个 HostsNagios
类,它包含一些特定于 nagios 的方法。
然后我正在做的事情是这样的:
nagios_hosts = nagios.gethosts()
lb_hosts = loadbalancer.gethosts()
hosts = nagios_hosts + lb_hosts
hosts.run_ssh_command_on_all_hosts('uname')
hosts.drain() # method of HostsLoadbalancer - drains just the loadbalancer-hosts
hosts.acknoledge_downtime() # method of NagiosHosts - does this just for the nagios hosts, is overlapping
这个问题的最佳解决方案是什么?
我想我可以以某种方式“复制所有方法” - 像这样: 对于 dir 中的 x(其他): setattr(self, x, getattr(other, x))
我走在正确的轨道上吗?或者我应该使用抽象基类?
I've got a base class where I want to handle __add__()
and want to support when __add__
ing two subclass instances - that is have the methods of both subclasses in the resulting instance.
import copy
class Base(dict):
def __init__(self, **data):
self.update(data)
def __add__(self, other):
result = copy.deepcopy(self)
result.update(other)
# how do I now join the methods?
return result
class A(Base):
def a(self):
print "test a"
class B(Base):
def b(self):
print "test b"
if __name__ == '__main__':
a = A(a=1, b=2)
b = B(c=1)
c = a + b
c.b() # should work
c.a() # should work
Edit: To be more specific: I've got a class Hosts
that holds a dict(host01=.., host02=..)
(hence the subclassing of dict
) - this offers some base methods such as run_ssh_commmand_on_all_hosts()
Now I've got a subclass HostsLoadbalancer
that holds some special methods such as drain()
, and I've got a class HostsNagios
that holds some nagios-specific methods.
What I'm doing then, is something like:
nagios_hosts = nagios.gethosts()
lb_hosts = loadbalancer.gethosts()
hosts = nagios_hosts + lb_hosts
hosts.run_ssh_command_on_all_hosts('uname')
hosts.drain() # method of HostsLoadbalancer - drains just the loadbalancer-hosts
hosts.acknoledge_downtime() # method of NagiosHosts - does this just for the nagios hosts, is overlapping
What is the best solution for this problem?
I think I can somehow "copy all methods" - like this:
for x in dir(other):
setattr(self, x, getattr(other, x))
Am I on the right track? Or should I use Abstract Base Classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,这是一个坏主意。您正在尝试将方法注入到类型中。话虽如此,您当然可以在 python 中执行此操作,但您必须意识到每次执行此操作时都希望创建一个新类型。这是一个示例:
更新
我已经更新了示例以删除手动移动方法——我们在这里使用 mixin。
In general this is a bad idea. You're trying to inject methods into a type. That being said, you can certainly do this in python, but you'll have to realize that you want to create a new type each time you do this. Here's an example:
UPDATE
I've updated the example to remove manually moving the methods -- we're using mixins here.
如果没有更多信息,很难回答你的问题。如果 Base 应该是所有类的公共接口,那么您可以使用简单继承来实现公共行为,同时保留子类的方法。例如,假设您需要一个基类,其中所有对象都具有
say_hola()
方法,但子类除了say_hola()
之外还可以具有任意其他方法:
C1
和C2
的所有实例除了其特定方法之外还有say_hola()
。更通用的模式是创建一个 Mixin。来自维基百科:
It is difficult to answer your question without more information. If
Base
is supposed to be a common interface to all classes, then you could use simple inheritance to implement the common behavior while preserving the methods of the subclasses. For instance, imagine that you need a Base class where all the objects have asay_hola()
method, but subclasses can have arbitrary additional methods in addition tosay_hola()
:This way all instances of
C1
andC2
havesay_hola()
in addition to their specific methods.A more general pattern is to create a
Mixin
. From Wikipedia: