我如何实现具有多个 init 继承的 super() ?
How can i implement super() with multiple init inheritances?
我正在做一个涉及多个继承的练习,初始化器需要多个参数并思考,super() 如何解决这个问题而不是手动调用每个 superclass?
class One:
def __init__(self, address, phone):
self.address = address
self.phone = phone
class Two:
def __init__(self, city):
self.city = city
class Three(One,Two):
def __init__(self, country, address, phone, city):
self.country = country
One.__init__(self, address, phone)
Two.__init__(self, city)
print(f"{address}, " + f"{phone}, " + f"{self.city}, " + f"{self.country}")
i = Three("Acountry", "AnAddress", "Aphone", "Acity")
这很好用,所有参数都按顺序打印,但我不知道如何在此处实现 super()
。
我试过在 subclass 上添加 2 个 superclass:
super().__init__(address, phone)
super().__init__(city)
甚至在父 class 上添加一个 super() 使其指向 class Two
:
class One:
def __init__(self, address, phone, city):
self.address = address
self.phone = phone
super().__init__(city)
class Two:
def __init__(self, city):
self.city = city
class Three(One,Two):
def __init__(self, country, address, phone, city):
self.country = country
super().__init__(address, phone)
print(f"{address}, " + f"{phone}, " + f"{self.city}, " + f"{self.country}")
i = Three("Acountry", "AnAddress", "Aphone", "Acity")
没用。
如何在有效的原始代码中实现 super()
?
您应该阅读 this article by Raymond Hettinger,其中介绍了 super
的工作原理,并概述了 类 使用它的外观。他建议将参数作为关键字参数传递,并在每个 __init__
:
的末尾调用 super().__init__
class One:
def __init__(self, address, phone, **kwargs):
self.address = address
self.phone = phone
super().__init__(**kwargs)
class Two:
def __init__(self, city, **kwargs):
self.city = city
super().__init__(**kwargs)
class Three(One,Two):
def __init__(self, country, address, phone, city, **kwargs):
self.country = country
super().__init__(address=address, phone=phone, city=city, **kwargs)
print(f"{address}, " + f"{phone}, " + f"{self.city}, " + f"{self.country}")
i = Three("Acountry", "AnAddress", "Aphone", "Acity")
我正在做一个涉及多个继承的练习,初始化器需要多个参数并思考,super() 如何解决这个问题而不是手动调用每个 superclass?
class One:
def __init__(self, address, phone):
self.address = address
self.phone = phone
class Two:
def __init__(self, city):
self.city = city
class Three(One,Two):
def __init__(self, country, address, phone, city):
self.country = country
One.__init__(self, address, phone)
Two.__init__(self, city)
print(f"{address}, " + f"{phone}, " + f"{self.city}, " + f"{self.country}")
i = Three("Acountry", "AnAddress", "Aphone", "Acity")
这很好用,所有参数都按顺序打印,但我不知道如何在此处实现 super()
。
我试过在 subclass 上添加 2 个 superclass:
super().__init__(address, phone)
super().__init__(city)
甚至在父 class 上添加一个 super() 使其指向 class Two
:
class One:
def __init__(self, address, phone, city):
self.address = address
self.phone = phone
super().__init__(city)
class Two:
def __init__(self, city):
self.city = city
class Three(One,Two):
def __init__(self, country, address, phone, city):
self.country = country
super().__init__(address, phone)
print(f"{address}, " + f"{phone}, " + f"{self.city}, " + f"{self.country}")
i = Three("Acountry", "AnAddress", "Aphone", "Acity")
没用。
如何在有效的原始代码中实现 super()
?
您应该阅读 this article by Raymond Hettinger,其中介绍了 super
的工作原理,并概述了 类 使用它的外观。他建议将参数作为关键字参数传递,并在每个 __init__
:
super().__init__
class One:
def __init__(self, address, phone, **kwargs):
self.address = address
self.phone = phone
super().__init__(**kwargs)
class Two:
def __init__(self, city, **kwargs):
self.city = city
super().__init__(**kwargs)
class Three(One,Two):
def __init__(self, country, address, phone, city, **kwargs):
self.country = country
super().__init__(address=address, phone=phone, city=city, **kwargs)
print(f"{address}, " + f"{phone}, " + f"{self.city}, " + f"{self.country}")
i = Three("Acountry", "AnAddress", "Aphone", "Acity")