在 Python 中覆盖超类时跳过参数
Skipping a parameter when overriding a superclass in Python
我正在尝试弄清楚如何创建一个子class,它跳过或不包含父 class 使用 __init__
和 [=16] 具有的属性=] 但我似乎无法理解它。
我尝试使用以下代码创建一个简单的继承:
class Animal:
def __init__(self, name, age, sound, **kwargs):
self.name = name
self.age = age
self.sound = sound
for key, value in kwargs.items():
setattr(self, key, value)
class Dog(Animal):
def __init__(self, name, age, sound, *args, **kwargs):
super().__init__(name, age, sound, **kwargs)
class Lion(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__(age, sound, **kwargs)
下面,我尝试为每个子 class(Dog
和 Lion
)打印基本的 attributes/information。它们的共同参数是 name
、age
和 sound
,它们都存在于我的 Dog
class 中(我还添加了 pet
和 breed
).
至于 Lion
class 因为它不需要任何名称(因为它通常生活在野外)我尝试跳过 name
参数所以我没有将其包含在 __init__
和 super()
.
中
当我 运行 文件时,它得到一个
TypeError: __init__()
missing 1 required positional argument: 'sound'.
dog = Dog('Lucky', 6, 'Arf! Arf!', pet=True, breed='Shih Tzu')
lion = Lion(10, 'Roar!', wild=True)
print("===Dog===")
print("Name: {}".format(dog.name))
print("Age: {}".format(dog.age))
print(dog.sound)
print("\n===Lion===")
print("Age: {}".format(lion.age))
print(lion.sound)
所以我尝试解决这些代码并在 Animal
class 中设置 sound=""
。
class Animal:
def __init__(self, name, age, sound="", **kwargs): # <--I added quotes
这次没有得到错误,但我没有得到正确的输出。
===Dog===
Name: Lucky
Age: 6
Arf! Arf!
===Lion===
Age: Roar!
我希望 Lion
像 Dog
一样在适当的位置具有正确的属性,除了不需要的名称。
我在代码中遗漏了什么吗?
简单的解决方法就是传递一个空的 name
,例如""
或 None
。那是因为如果你将它们设为强制性,你就不能跳过参数!就像您在 Animal
的 __init__
中所做的那样。
class Lion(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__("", age, sound, **kwargs)
然而,也许更好的解决方法是使 name
成为可选参数,因为 Animal
可能有名称但不需要名称。永远记住 类 尝试抽象 "real concepts":
class Animal:
def __init__(self, age, sound, **kwargs):
self.age = age
self.sound = sound
for key, value in kwargs.items():
setattr(self, key, value)
class Dog(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__(age, sound, **kwargs)
class Lion(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__(age, sound, **kwargs)
dog = Dog(6, 'Arf! Arf!', name='Lucky', pet=True, breed='Shih Tzu')
lion = Lion(10, 'Roar!', wild=True)
print("===Dog===")
print("Name: {}".format(dog.name))
print("Age: {}".format(dog.age))
print(dog.sound)
print("\n===Lion===")
print("Age: {}".format(lion.age))
print(lion.sound)
两种方法都给出了正确的结果(据我所知):
===Dog===
Name: Lucky
Age: 6
Arf! Arf!
===Lion===
Age: 10
Roar!
我正在尝试弄清楚如何创建一个子class,它跳过或不包含父 class 使用 __init__
和 [=16] 具有的属性=] 但我似乎无法理解它。
我尝试使用以下代码创建一个简单的继承:
class Animal:
def __init__(self, name, age, sound, **kwargs):
self.name = name
self.age = age
self.sound = sound
for key, value in kwargs.items():
setattr(self, key, value)
class Dog(Animal):
def __init__(self, name, age, sound, *args, **kwargs):
super().__init__(name, age, sound, **kwargs)
class Lion(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__(age, sound, **kwargs)
下面,我尝试为每个子 class(Dog
和 Lion
)打印基本的 attributes/information。它们的共同参数是 name
、age
和 sound
,它们都存在于我的 Dog
class 中(我还添加了 pet
和 breed
).
至于 Lion
class 因为它不需要任何名称(因为它通常生活在野外)我尝试跳过 name
参数所以我没有将其包含在 __init__
和 super()
.
当我 运行 文件时,它得到一个
TypeError:
__init__()
missing 1 required positional argument: 'sound'.
dog = Dog('Lucky', 6, 'Arf! Arf!', pet=True, breed='Shih Tzu')
lion = Lion(10, 'Roar!', wild=True)
print("===Dog===")
print("Name: {}".format(dog.name))
print("Age: {}".format(dog.age))
print(dog.sound)
print("\n===Lion===")
print("Age: {}".format(lion.age))
print(lion.sound)
所以我尝试解决这些代码并在 Animal
class 中设置 sound=""
。
class Animal:
def __init__(self, name, age, sound="", **kwargs): # <--I added quotes
这次没有得到错误,但我没有得到正确的输出。
===Dog===
Name: Lucky
Age: 6
Arf! Arf!
===Lion===
Age: Roar!
我希望 Lion
像 Dog
一样在适当的位置具有正确的属性,除了不需要的名称。
我在代码中遗漏了什么吗?
简单的解决方法就是传递一个空的 name
,例如""
或 None
。那是因为如果你将它们设为强制性,你就不能跳过参数!就像您在 Animal
的 __init__
中所做的那样。
class Lion(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__("", age, sound, **kwargs)
然而,也许更好的解决方法是使 name
成为可选参数,因为 Animal
可能有名称但不需要名称。永远记住 类 尝试抽象 "real concepts":
class Animal:
def __init__(self, age, sound, **kwargs):
self.age = age
self.sound = sound
for key, value in kwargs.items():
setattr(self, key, value)
class Dog(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__(age, sound, **kwargs)
class Lion(Animal):
def __init__(self, age, sound, *args, **kwargs):
super().__init__(age, sound, **kwargs)
dog = Dog(6, 'Arf! Arf!', name='Lucky', pet=True, breed='Shih Tzu')
lion = Lion(10, 'Roar!', wild=True)
print("===Dog===")
print("Name: {}".format(dog.name))
print("Age: {}".format(dog.age))
print(dog.sound)
print("\n===Lion===")
print("Age: {}".format(lion.age))
print(lion.sound)
两种方法都给出了正确的结果(据我所知):
===Dog===
Name: Lucky
Age: 6
Arf! Arf!
===Lion===
Age: 10
Roar!