- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于在子类上强制执行某些不变量的元类或 __init_subclass__
应该引发什么异常,是否有任何好的约定?
对于这个答案,我会接受来自 Python 内置行为、PEP、标准库或完善的第三方库的先例,或者关于为什么应该以某种方式完成的令人信服的推理。
最佳答案
我所知道的 Python 本身的先例是总是引发 TypeError
:
TypeError
: >>> class MorePowerfulTuple(tuple): pass
... __slots__ = ('power',)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: nonempty __slots__ not supported for subtype of 'tuple'
bool
,它会引发一个 TypeError
: >>> class BetterBool(bool): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'bool' is not an acceptable base type
>>> class StrongerFasterMethodType(types.MethodType):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'bound_method' is not an acceptable base type
这里最重要的先例设定者是第一个。后两个可以说是特定于类型的子类化错误,因为它们是由于尝试使用错误的 type 作为子类化操作的输入而导致的特定错误(因此,如果您只有这些示例,那仍然是一致的如果由于某些其他原因导致子类化错误,则使用其他异常类型更合适)。但是第一个是引发 TypeError
,即使问题不一定是特定于类型的:您可以将 __slots__ = ('power',)
替换为 __slots__ = ()
并且它会起作用。因此,我们在语言本身的引用实现中至少有一个子类错误被报告为 TypeError
,即使在许多其他情况下,使用空可迭代而不是可迭代一个或多个元素更适合作为 ValueError
。
the built-in exception hierarchy里面好像没有什么类专为此目的而设计。
此外,该文档排除了尝试使用多重继承来解决此类子类错误(在某些情况下这可能很诱人 - 例如,元类或 的实现者__init_subclass__
可能会想象一个工厂函数根据某些参数在程序执行过程中动态生成子类,并认为引发更精确的错误如 ValueError
会有所帮助 - 所以开发人员可能会认为将子类 TypeError
和 ValueError
以及他们自己的子类错误和 ValueError
相乘是个好主意,而不是仅仅引发一个普通的TypeError
就像上面的内置类 precent 所做的那样),因为该文档明确建议不要从多个异常类型进行子类化,因为内部实现细节可以自由地以一种可能无法实现的方式实现不同的异常将它们都用作基类:
Some have custom memory layouts which makes it impossible to create a subclass that inherits from multiple exception types. The memory layout of a type is an implementation detail and might change between Python versions, leading to new conflicts in the future. Therefore, it’s recommended to avoid subclassing multiple exception types altogether.
我不知道有任何 PEP、标准库或第三方库建立了特定的实践。
(但是,肯定有一些库,例如 namedtuple
、dataclasses
、attrs
,甚至可能还有像 SQLAlchemy 和 Pydantic 这样的库,它们基于用户参数以编程方式生成类,这对于他们自己的示例可能值得一看。)
关于Python:元类或 `__init_subclass__`:错误子类创建期间的惯用异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72639982/
我在修补自定义类时遇到问题'__init_subclass__ .我认为这与我将修补函数绑定(bind)到类的方式有关: def _patched_initsubclass(cls, **kwargs
我四处寻找这个问题的答案,但找不到任何东西。如果之前有人问过这个问题,我深表歉意。 我知道有 3-4 种方法可以从父类强制执行子类的给定方法(编辑元类的 __new__ 方法,挂接到 builtins
我终于升级了我的 python 版本,并且发现了添加的新功能。除其他外,我对新的 __init_subclass__ 摸不着头脑。方法。来自文档: This method is called when
对于在子类上强制执行某些不变量的元类或 __init_subclass__ 应该引发什么异常,是否有任何好的约定? 对于这个答案,我会接受来自 Python 内置行为、PEP、标准库或完善的第三方库的
我想创建一个设置注册表。我还希望能够将设置分组到宏类别中。 以下简化示例适用于单个注册表: class RegistryHolder: registry = {} def __init
我在一个项目中使用 init_subclass ,当我遇到内置方法时有点犹豫,当代码第一次在解释器中运行时启动——没有通过实例化直接引用包含类或它枚举的子类。 谁能告诉我这是怎么回事,并指出任何安全使
我正在尝试让类装饰器工作。装饰器将向其所应用的类添加一个 __init_subclass__ 方法。 但是,当该方法动态添加到类中时,第一个参数不会绑定(bind)到子类对象。为什么会发生这种情况?
使用 type() 创建动态类时,如何将值传递给 __init__subclass__() 中的 kwargs 参数 class Base(): def __init_subclass__(c
我正在尝试编写一个简单的插件系统。 a)为此,我使用 3.6 魔术函数 __init_subclass__ https://stackoverflow.com/a/41924331/1506569 找
我注意到我不能以我想要的方式将 __init_subclass__ 与 Django 模型类一起使用。看起来元类在父类的 __init_subclass__ 方法运行时还没有完成子类的创建。虽然我明白
我的问题灵感来自这个 question . 问题在于 3 级类模型 - 终止类(3 级)仅应存储在注册表中,但 2 级是干扰性的并且也已存储,因为它们是 1 级的子类。 我想通过使用元类摆脱 1 级类
我正在尝试创建一个元类,但是当我将它分配给另一个类时,我收到错误 TypeError: __init_subclass__() takes no keyword arguments但我没有实现任何 _
我有以下代码来比较基类的当前(空)所需功能的实现与其子类,子类必须以某种不同的方式实现它们,以便在运行时被认为是可接受的。如果不使用 metaclass=ABCMeta 并在这些基类方法上实现 @ab
这个问题在这里已经有了答案: Why does a classmethod's super need a second argument? (1 个回答) 3年前关闭。 我尝试从 __init_sub
我正在测试一些 pyqt 小部件及其方法,但当我创建一个继承 QLineEdit 类的子类以便在单击鼠标右键时创建上下文菜单时,我一直遇到问题。此类将主窗口作为父窗口。 运行此代码会产生以下输出: T
我正在尝试将 python 3.6 的新 __init_subclass__ 功能 ( PEP 487 ) 与 abc 模块一起使用。它似乎没有用。以下代码: from abc import ABCM
我是一名优秀的程序员,十分优秀!