为了说明我的问题,这里有三个模块:
这是模块A
'''
lets call this module the parent, it regroups multiple classes with several
method each
'''
class Rectangle():
'''
The basic rectangle class that is parent for other classes in this
module
'''
def __init__(self, x_length, y_length):
self.x_length = x_length
self.y_length = y_length
def create_points(self):
'''
Basic geometrical method
'''
self.point_1 = [x_length/2, y_length/2]
self.point_2 = [-x_length/2, y_length/2]
self.point_3 = [-x_length/2, -y_length/2]
self.point_4 = [x_length/2, -y_length/2]
class Square(Rectangle):
'''
The square that is a rectangle with two identical sides
'''
def __init__(self, side_dim):
super().__init__(side_dim, side_dim)
class SquareCollection():
'''
Creates a composition relation with an other class of the module
'''
def __init__(self, dim_list):
'''
The constructor creates a square for every float in the given list
'''
for val in dim_list:
try:
self.element.append(Square(val))
except AttributeError:
self.element = [Square(val)]
def create_points(self):
for elmt in self.element:
elmt.create_points()
这是模块B1
'''
lets call this module the child 1, it redefines the classes from the parent but
with an additionnal method specific for its use case
'''
import Module_A as ma
class Rectangle(ma.Rectangle):
'''
The basic rectangle class that is parent for other classes in this
module
'''
def module_specific_method(self):
self.special_attribute_1 = True
class Square(ma.Square):
'''
The square that is a rectangle with two identical sides
'''
def module_specific_method(self):
self.special_attribute_1 = True
class SquareCollection(ma.SquareCollection):
'''
Redefining the SquareCollection with an additionnal method
'''
def module_specific_method(self):
for elmt in self.element:
elmt.module_specific_method()
这是模块B2
'''
lets call this module the child 2, it redefines the classes from the parent but
with an additionnal method specific for its use case
This module is a copy of Module_B2 and is used to justify the used code
structure
'''
import Module_A as ma
class Rectangle(ma.Rectangle):
'''
The basic rectangle class that is parent for other classes in this
module
'''
def module_specific_method(self):
self.special_attribute_2 = True
class Square(ma.Square):
'''
The square that is a rectangle with two identical sides
'''
def module_specific_method(self):
self.special_attribute_2 = True
class SquareCollection(ma.SquareCollection):
'''
Redefining the SquareCollection with an additionnal method
'''
def module_specific_method(self):
for elmt in self.element:
elmt.module_specific_method()
当我导入模块B2时,创建一个sc = SquareCollection([4, 3.5, 0.8])
实例,然后运行sc.module_specific_method()
时,我会为使用a模块创建的被调用的Square
类获得AttributeError
,因为SquareCollection
类继承自a模块中定义的类,a模块本身基于同一模块中的类创建多个Square
实例。AttributeError
是预期的,因为在A模块中我没有定义这个module_specific_method
。
由于我的代码结构以及我目前使用它的方式,我使用的是模块B1或模块B2。我目前通过两次重写模块A中包含的所有内容来避免这个问题(一次在B1中,另一次在B2中(。因为我正在重构我所有的代码,我希望我能删除所有重复的代码,并将其放在";普通的";模块,如上面用简化示例所示。
模块A中的类如何可能继承/指向我调用它的B模块?
我希望我的问题是明确的,因为我真的很难将其正式化
干杯!
您可以通过使基本SquareCollection
中使用的Square
类成为集合类的属性来实现这一点,这样它就不会被硬编码,而是可以被子类显式重写:
# Module_A
class Square:
pass # implement stuff here
class SquareCollection
BaseItem = Square # the "class that is collected here"
def __init__(self, dim_list):
# spawn BaseItem instances here, which are Square by default,
# but might be set to something else in a subclass
self.element = [self.BaseItem(val) for val in dim_list]
# Module_B1
import Module_A as ma
class Square(ma.Square):
pass
class SquareCollection(ma.SquareCollection):
# override the collection's BaseItem with this module's Square class,
# but keep the rest of the SquareCollection code the same
BaseItem = Square