使用 ABC 模块时,关键字参数是一种好习惯吗?
When using the ABC module, are keyword arguments a good practice?
这个问题是 的后续问题。
将 super()
用于 multiple inheritance 时,建议的方法是使用关键字参数将剩余值向上传递到调用链中。
当使用 ABC
模块时,在 super().__init__
方法中做同样的事情是好的做法吗?
关于 super()
的 Python 文档链接到的博客 post 没有提及任何关于使用 **kwargs
和 ABC
模块的内容.它专注于具体 类 的多重继承。换句话说我的问题,关于使用 **kwargs
和 super()
的建议是否适用于使用 ABC
模块的 类?
例如:
from abc import ABC
class GameWeapon(ABC):
def __init__(self, name, damage, **av):
super().__init__(**av)
self.name = name
self.required_strength = required_strength
self.damage = damage
class ChargeGun(GameWeapon):
def __init__(self, name, required_strength, damage, **av):
super().__init__(name=name,damage=damage,**av)
让我们来看看您引用的博客 post 中的特定实例。
class Shape:
def __init__(self, shapename, **kwds):
self.shapename = shapename
super().__init__(**kwds)
class ColoredShape(Shape):
def __init__(self, color, **kwds):
self.color = color
super().__init__(**kwds)
cs = ColoredShape('red', shapename='circle', radius=30)
TypeError: object.__init__() takes no arguments
当我们创建一个ColoredShape
对象时,它会要求我们输入颜色和形状名称。如果你传递了一个意外的关键字参数,它会给你一个错误。这是因为所有 classes by default(在 python 3 中)都继承自内置类型 object
,它有一个不需要参数的 __init__
。
正如文章指出的那样,object
保证是 MRO 中调用的最后一个 class。但是,如果您在 Shape 中删除对 super 的调用,则可以毫无问题地添加任意数量的关键字参数,即使它们不会用于任何用途。
class Shape:
def __init__(self, shapename, **kwds):
self.shapename = shapename
class ColoredShape(Shape):
def __init__(self, color, **kwds):
self.color = color
super().__init__(**kwds)
cs = ColoredShape('red', shapename='circle', radius=30, diameter=60)
在您 post 编写的代码中,您继承自 abc,它不会通过 super 对 Object 的 init 进行最终调用。因此,博客 post 中显示的设计模式不适用于您的情况。希望对您有所帮助。
这个问题是
将 super()
用于 multiple inheritance 时,建议的方法是使用关键字参数将剩余值向上传递到调用链中。
当使用 ABC
模块时,在 super().__init__
方法中做同样的事情是好的做法吗?
关于 super()
的 Python 文档链接到的博客 post 没有提及任何关于使用 **kwargs
和 ABC
模块的内容.它专注于具体 类 的多重继承。换句话说我的问题,关于使用 **kwargs
和 super()
的建议是否适用于使用 ABC
模块的 类?
例如:
from abc import ABC
class GameWeapon(ABC):
def __init__(self, name, damage, **av):
super().__init__(**av)
self.name = name
self.required_strength = required_strength
self.damage = damage
class ChargeGun(GameWeapon):
def __init__(self, name, required_strength, damage, **av):
super().__init__(name=name,damage=damage,**av)
让我们来看看您引用的博客 post 中的特定实例。
class Shape:
def __init__(self, shapename, **kwds):
self.shapename = shapename
super().__init__(**kwds)
class ColoredShape(Shape):
def __init__(self, color, **kwds):
self.color = color
super().__init__(**kwds)
cs = ColoredShape('red', shapename='circle', radius=30)
TypeError: object.__init__() takes no arguments
当我们创建一个ColoredShape
对象时,它会要求我们输入颜色和形状名称。如果你传递了一个意外的关键字参数,它会给你一个错误。这是因为所有 classes by default(在 python 3 中)都继承自内置类型 object
,它有一个不需要参数的 __init__
。
正如文章指出的那样,object
保证是 MRO 中调用的最后一个 class。但是,如果您在 Shape 中删除对 super 的调用,则可以毫无问题地添加任意数量的关键字参数,即使它们不会用于任何用途。
class Shape:
def __init__(self, shapename, **kwds):
self.shapename = shapename
class ColoredShape(Shape):
def __init__(self, color, **kwds):
self.color = color
super().__init__(**kwds)
cs = ColoredShape('red', shapename='circle', radius=30, diameter=60)
在您 post 编写的代码中,您继承自 abc,它不会通过 super 对 Object 的 init 进行最终调用。因此,博客 post 中显示的设计模式不适用于您的情况。希望对您有所帮助。