我正在尝试用python2.6创建一个可重用的应用程序。你知道吗
我正在开发用于监听GPS跟踪设备的服务器端脚本。脚本正在使用套接字。你知道吗
我有一个基类,它定义了处理设备发送的数据的基本方法。你知道吗
class MyDevice(object):
def __init__(self, db)
self.db = db # This is the class that defines methods for connecting/using database
def initialize_db(self):
...
def handle_data(self):
self.initialize_db()
...
self.process_data()
def process_data(self):
...
self.categorize_data()
def categorize_data(self):
...
self.save_data()
def save_data(self):
...
这个基类服务于许多设备,因为这些设备之间只有一些细微的差别。因此,我为每个特定的设备类型创建一个类,并对该设备进行特定的安排。你知道吗
class MyDeviceType1(Mydevice):
def __init__(self, db):
super(MyDeviceType1, self).__init__(db)
def categorize_data(self):
super(MyDeviceType1, self).categorize_data(self)
self.prepopulate_data()
... # Do other operations specific to device
def prepopulate_data(self):
"""this is specific to Type1 devices"""
...
class MyDeviceType2(Mydevice):
def __init__(self, db):
super(MyDeviceType1, self).__init__(db)
def categorize_data(self):
super(MyDeviceType1, self).categorize_data(self)
self.unpopulate_data()
... # Do other operations specific to device
def unpopulate_data(self):
"""this is specific to Type2 devices"""
...
我有监听特定套接字的套接字侦听器,并调用相关类(MyDeviceType1
或MyDeviceType2
),如:
conn, address = socket.accept()
...
thread.start_new_thread(MyDeviceType1(db_connector).handle_data, ())
这种结构对我来说很好也很有用。一个设备(MyDevice)可能有许多继承基类的子类型(MyDeviceType1,MyDeviceType2)。你知道吗
而且基本设备不止一种。因此有其他设备和子类型其他设备类型1等
MyDevice
和OtherDevice
的工作方式完全不同,因此它们都是基本类型,而它们的底层代码也完全不同。你知道吗
我还有一些附加功能。这些功能可由几乎所有设备基本类型的一个或两个子类型使用。你知道吗
所以我想准备一个可重用(可插入)的类,它可以被任何需要这些功能的子类型继承。你知道吗
class MyAddOn(object):
def remove_unusable_data(self):
...
def categorize_data(self):
super ???
self.remove_unusable_data()
这是我坚持的部分。因为这是一个独立的模块,它不应该从MyDevice
或OtherDevice
等继承,但不是所有的子设备类型都在使用这些功能,我也不能从MyAddOn
继承MyDevice
。你知道吗
只有逻辑方法是这样的,从MyDevice
和MyAddOn
继承子类型MyDeviceSubType1
class MyDeviceType1(Mydevice, MyAddOn):
def __init__(self, db):
super(MyDeviceType1, self).__init__(db)
def categorize_data(self):
>> super(MyDeviceType1, self).categorize_data(self) <<
self.prepopulate_data()
... # Do other operations specific to device
def prepopulate_data(self):
"""this is specific to Type1 devices"""
super(MyDeviceType1, self).categorize_data(self)
是问题所在。super
正在触发Mydevice.categorize_data
而不是MyAddOn.categorize_data
有没有办法使用super
调用触发MyAddOn
方法,或者不需要单独调用该类方法?MyDevice.categorize_data
和MyAddOn.categorize_data
都应该被调用。你知道吗
这在python中称为协作多重继承,可以很好地工作。你知道吗
你所说的“Addon”类,通常被称为“Mixin”。你知道吗
只需在Mixin类中调用
super
方法:我想指出一些事情:
在这里调用
super
似乎有悖常理,因为MyAddon
的父级没有名为categorize_data
的属性,您可能会期望此表示法失败。你知道吗这就是
<>与C++ +EEM>或java EEE>不同,^ {< CD1>}函数不一定调用类的父类。实际上,不可能预先知道super
函数发挥作用的地方。Some认为这种行为是python的优点。你知道吗super
将调用哪个函数,因为它将在运行时根据method resoltion order决定。你知道吗python中的
super
实际上应该被称为next
,因为它将调用继承树中的下一个方法。你知道吗对于mixin,调用super尤其重要,即使您是从
object
继承的。你知道吗欲了解更多信息,我建议观看Raymond Hettinger在pycon 2015的Super considered Super上的精彩演讲。你知道吗
在python中使用它是一个很好的模式。以下是我在编写遵循open-closed principle规则的结构化应用程序时经常遇到的一种模式:
我有一个用于生产的库类:
现在您得到一个特性请求,在特定情况下,
BaseClassA
和BaseClassB
都应该实现特性X根据open-close,实现这个特性不需要接触现有的代码,根据DRY,不应该重复代码。你知道吗
解决方案是创建
FeatureMixin
并创建从基类和mixin继承的空子类:相关问题 更多 >
编程相关推荐