继承:如何使 parent 方法与其他 parent 方法一起工作?
Inheritance: How to make parent method work with other parent methods?
所以我有两个 classes,其中一个继承自另一个并覆盖所有 parent 方法:
class Parent:
def name(self):
return 'parent'
def run(self):
print(f'calling method from parent: I am {self.name()}')
class Child(Parent):
def name(self):
return 'child'
def run(self):
print(f'calling method from child: I am {self.name()}')
return super().run()
运行 以下片段 Child().run()
触发方法 run
都用于 child 和 parent,输出为:
calling method from child: I am child
calling method from parent: I am child
结果很明确 - 因为我们重新定义了方法 name
,新版本在两个 run
方法中使用。 (I am child
两行)
这是主要问题 - 我希望它的工作方式是 parent run
方法使用 parent name
.
到目前为止我所做的是用 self.__class__.__mro__[1]
替换 super
,所以该方法看起来像
def run(self):
print(f'calling method from child: I am {self.name()}')
return self.__class__.__mro__[1]().run()
它的工作方式是使用方法解析顺序获取 parent class 并创建 parent class 的实例。它工作正常,现在结果是:
calling method from child: I am child
calling method from parent: I am parent
但我不喜欢这个解决方案:
- 单一继承 - 因为我们硬编码 parent class 索引,我们不能让它与多个 parent classes
一起工作
- 在这种情况下使用 MRO 感觉不合适
- 这里我们假设
__init__
不接受额外的参数
我认为线索在于改变 parent 方法中的 self.name
以便它明确地使用 parent 方法,但我不知道如何实现这一点。
您可以使用 __class__
:
找到方法定义的当前 class
class Parent:
def name(self):
return 'parent'
def run(self):
print(f'calling method from parent: I am {__class__.name(self)}')
我建议将 name
设为 name-mangled (__
) 属性,该属性对 class:
是私有的
class Parent:
__name = 'parent'
def run(self):
print(f'calling method from parent: I am {self.__name}')
class Child(Parent):
__name = 'child'
def run(self):
print(f'calling method from child: I am {self.__name}')
return super().run()
Child().run()
# calling method from child: I am child
# calling method from parent: I am parent
所以我有两个 classes,其中一个继承自另一个并覆盖所有 parent 方法:
class Parent:
def name(self):
return 'parent'
def run(self):
print(f'calling method from parent: I am {self.name()}')
class Child(Parent):
def name(self):
return 'child'
def run(self):
print(f'calling method from child: I am {self.name()}')
return super().run()
运行 以下片段 Child().run()
触发方法 run
都用于 child 和 parent,输出为:
calling method from child: I am child
calling method from parent: I am child
结果很明确 - 因为我们重新定义了方法 name
,新版本在两个 run
方法中使用。 (I am child
两行)
这是主要问题 - 我希望它的工作方式是 parent run
方法使用 parent name
.
到目前为止我所做的是用 self.__class__.__mro__[1]
替换 super
,所以该方法看起来像
def run(self):
print(f'calling method from child: I am {self.name()}')
return self.__class__.__mro__[1]().run()
它的工作方式是使用方法解析顺序获取 parent class 并创建 parent class 的实例。它工作正常,现在结果是:
calling method from child: I am child
calling method from parent: I am parent
但我不喜欢这个解决方案:
- 单一继承 - 因为我们硬编码 parent class 索引,我们不能让它与多个 parent classes 一起工作
- 在这种情况下使用 MRO 感觉不合适
- 这里我们假设
__init__
不接受额外的参数
我认为线索在于改变 parent 方法中的 self.name
以便它明确地使用 parent 方法,但我不知道如何实现这一点。
您可以使用 __class__
:
class Parent:
def name(self):
return 'parent'
def run(self):
print(f'calling method from parent: I am {__class__.name(self)}')
我建议将 name
设为 name-mangled (__
) 属性,该属性对 class:
class Parent:
__name = 'parent'
def run(self):
print(f'calling method from parent: I am {self.__name}')
class Child(Parent):
__name = 'child'
def run(self):
print(f'calling method from child: I am {self.__name}')
return super().run()
Child().run()
# calling method from child: I am child
# calling method from parent: I am parent