python - Python继承,更改基类的初始化成员
问题描述
如果我有一个这样的继承类:
phonebook = {"John": 123, "Rose": 234, "Miles": 345}
class Base(AbstractBase):
def __init__(self):
self.phonebook = phonebook
def doSomething(self):
aFunctionThatDoesSomething(phonebook)
如果我想使用不同的电话簿,如何继承这个类并调用该函数?我知道我可以将其添加为参数,但是有没有办法在不将其添加到init的情况下做到这一点?
理想情况下,我想编写一个使用已初始化的新值的函数覆盖。所以是这样的:
class Derived(Base):
def aFunctionThatDoesSomething(self):
# I want to add some names to the phonebook in the base class here
self.phonebook = addContacts(self.phonebook)
super().aFunctionThatDoesSomething()
解决方案
推荐阅读
- java - 适用于 Java 的 Microsoft Graph SDK - InvalidAuthenticationToken 错误 80049217
- io - 如何识别系统的资源绑定
- google-cloud-firestore - 我被黑了吗?Cloud Firestore巨大的流量选择差距,没有解释
- python - 如何让菜单选项在 WxPython 中做不同的事情?
- python - 将 Excel 表格作为图像粘贴到 Word (Python)
- c - 带有结构和类型定义的 c 头文件和 .c 文件
- android - Pixel 4a - 找不到 ARCore,即使它说已经下载
- php - PHP搜索中超过2个OR条件
- mysql - Retaining earliest instances of duplicate entries from a table
- delphi - How to Use Delphi Translation Editor?