Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中向第三方类添加属性的建议方法_Python_Inheritance - Fatal编程技术网

在python中向第三方类添加属性的建议方法

在python中向第三方类添加属性的建议方法,python,inheritance,Python,Inheritance,在python中,假设我有许多类a、B、C。。。所有源于类“Base”。 我需要向所有这些派生类添加一个新属性(及其相关方法),而无需修改基类。然后在init函数中设置属性 我正在考虑添加另一个基类,如 class NewBase: __init__(self, ...): self.new_property = ... class A1(A, NewBase): class B1(B, NewBase): class C1(C, NewBase): 但是多重继承

在python中,假设我有许多类a、B、C。。。所有源于类“Base”。 我需要向所有这些派生类添加一个新属性(及其相关方法),而无需修改基类。然后在init函数中设置属性

我正在考虑添加另一个基类,如

class NewBase:
    __init__(self, ...):
        self.new_property = ...

class A1(A, NewBase):

class B1(B, NewBase):

class C1(C, NewBase):

但是多重继承带来了其他问题。
有没有建议的方法?

注意:在上面的问题中,
BaseClass
或继承的
a
B
类都可以修改

目标

我们想在其构造函数的
BaseClass
中添加一个新属性,它是
NewClass
实例

解决方案

让我们定义我们的
基类
及其继承
A
B
类:

class BaseClass:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        print(f'In BaseClass init, got parameters {self.x} {self.y}')

class A(BaseClass):
    def __init__(self, x, y):
        BaseClass.__init__(self, x, y)

class B(BaseClass):
    def __init__(self, x, y):
        BaseClass.__init__(self, x, y)
以及我们要添加为属性的
NewClass

class NewClass:
    def __init__(self):
        self.new_class_property = 'sabich'
        print('In NewClass init')
    def func1(self):
        print('In func1')
    def func2(self):
        print('In func2')
现在,我们可以使用
基类访问
基类
构造函数。\uuuu init\uuuu
并使用decorator将附加逻辑包装在其周围:

# This decorator wraps adding the new parameter in the constructor
def init_decorator(fun):
    # This would be the returned constructor
    def ret_fun(self, *args, **kwargs):
        # Adding parameter to `NewClass`
        self.parameter = NewClass()
        # Calling the real constructor
        return fun(self, *args, **kwargs)
    return ret_fun

BaseClass.__init__ = init_decorator(BaseClass.__init__)
通过创建类型为
A
的对象,我们可以看到它的参数属性为
NewClass

# In NewClass init
# In BaseClass init, got parameters 1 2
a = A(1, 2)

# sabich
print(a.parameter.new_class_property)
全部代码:

class BaseClass:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        print(f'In BaseClass init, got parameters {self.x} {self.y}')

class A(BaseClass):
    def __init__(self, x, y):
        BaseClass.__init__(self, x, y)

class B(BaseClass):
    def __init__(self, x, y):
        BaseClass.__init__(self, x, y)

class NewClass:
    def __init__(self):
        self.new_class_property = 'sabich'
        print('In NewClass init')
    def func1(self):
        print('In func1')
    def func2(self):
        print('In func2')

def init_decorator(fun):
    def ret_fun(self, *args, **kwargs):
        self.parameter = NewClass()
        return fun(self, *args, **kwargs)
    return ret_fun

BaseClass.__init__ = init_decorator(BaseClass.__init__)

# In NewClass init
# In BaseClass init, got parameters 1 2
a = A(1, 2)

# sabich
print(a.parameter.new_class_property)

将新类的逻辑与从
基类继承的类融合后,您希望从类中得到什么?公开新的类方法和封装继承类(A、B、C)是否合理?或者您更愿意将继承类的方法和新类的方法都保留为混合类?请详细说明新的类逻辑如何与继承类交互。当你说属性时,你实际上是指属性吗?因为属性是一种非常特定的东西,实际上是添加到类中的,而不是添加到实例中的。什么问题?你是否真的遇到了一些,或者这只是道听途说?请发布一个包含所有相关类的实际MCVE,并显示所需的输出。我认为你对术语的掌握还不够精确,目前还不能只用散文。但这只是学习和改进的一部分。@AvivYaniv我想要的是向基类添加一个属性,该属性必须在init中设置,并在所有派生类中使用。但是我不能修改现有的工具。使用decorator是一个创造性的想法。它是否与super()调用一起工作,但不完全调用基类方法。另外,添加的属性需要设置一个新参数init func。谢谢@PasserbyD。我不明白你所说的“它能与super()调用一起工作,但不能准确地调用基类方法”是什么意思。关于向新属性传递参数,如果可以使用依赖项注入进行传递,则更可取,因为直接传递参数需要对所有继承类A、B等执行此技巧。。。