无论如何要从其他模块覆盖 class,同时将它的方法保留在 python 中
Is there anyway to override class from other module while keeping it's methods in python
为了说明我的问题,这里有3个模块:
这是模块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()
我得到一个 AttributeError
用于被调用的 Square
classes 是使用 A 模块创建的,因为 SquareCollection
class 继承自 A 模块中定义的那个,它本身根据 class 定义创建多个 Square
实例在同一个模块中。 AttributeError
是预期的,因为我在 A 模块的任何地方都定义了这个 module_specific_method
.
由于我的代码结构和我目前的使用方式,我使用的是模块 B1 或模块 B2。我目前通过重写模块 A 中包含的所有内容两次(一个在 B1 中,另一个在 B2 中)来避免这个问题。因为我正在重构我所有的代码,所以我希望我可以删除所有重复的代码并将其放入一个“通用”模块中,如上面的简化示例所示。
模块 A 中的 classes 怎么可能 inherit/point 回到我调用它的 B 模块?
我希望我的问题很清楚,因为我真的很难把它形式化。
干杯!
您可以通过使基础 SquareCollection
中使用的 Square
class 成为集合 class 的属性来实现此目的,这样它就不会被硬编码,而是变成也可以被 subclasses 显式覆盖:
# 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
为了说明我的问题,这里有3个模块:
这是模块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()
我得到一个 AttributeError
用于被调用的 Square
classes 是使用 A 模块创建的,因为 SquareCollection
class 继承自 A 模块中定义的那个,它本身根据 class 定义创建多个 Square
实例在同一个模块中。 AttributeError
是预期的,因为我在 A 模块的任何地方都定义了这个 module_specific_method
.
由于我的代码结构和我目前的使用方式,我使用的是模块 B1 或模块 B2。我目前通过重写模块 A 中包含的所有内容两次(一个在 B1 中,另一个在 B2 中)来避免这个问题。因为我正在重构我所有的代码,所以我希望我可以删除所有重复的代码并将其放入一个“通用”模块中,如上面的简化示例所示。
模块 A 中的 classes 怎么可能 inherit/point 回到我调用它的 B 模块?
我希望我的问题很清楚,因为我真的很难把它形式化。
干杯!
您可以通过使基础 SquareCollection
中使用的 Square
class 成为集合 class 的属性来实现此目的,这样它就不会被硬编码,而是变成也可以被 subclasses 显式覆盖:
# 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