isinstance 如何为 List 工作? [英] How does isinstance work for List?

查看:49
本文介绍了isinstance 如何为 List 工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 Python 的类型注释是如何工作的(例如 ListDict - not listdict).具体来说,我对 isinstance(list(), List) 的工作原理很感兴趣,以便我可以创建自己的自定义注释.

I'm trying to understand how Python's type annotations work (e.g. List and Dict - not list or dict). Specifically I'm interested in how isinstance(list(), List) works, so that I can create my own custom annotations.

我看到 List 被定义为:

class List(list, MutableSequence[T], extra=list):
    . . .

我熟悉metaclass = xxx,但我找不到关于此extra = xxx 的任何文档.这是一个关键字还是只是一个参数,如果是这样,它来自哪里,它是否符合我的要求?它甚至与 isinstance 相关吗?

I'm familiar with metaclass = xxx but I can't find any documentation on this extra = xxx. Is this a keyword or just an argument, and if so, where does it come from and does it do what I'm after? Is it even relevant for isinstance?

推荐答案

isinstance()issubclass()hooks 中有 hooksa href="https://docs.python.org/3/reference/datamodel.html#class.__instancecheck__" rel="noreferrer">object.__instancecheck__()object.__subclasscheck__() 表示typing 泛型也使用.

The isinstance() and issubclass() have hooks in object.__instancecheck__() and object.__subclasscheck__() that the typing generics also use.

如果你想提供自己的泛型,你真的要研究typing 模块源代码,具体如何GenericMetaGeneric 用于定义其他 Generic 类型,如 List;大多数此类检查委托给abc.ABCMeta.__subclasshook__.您可以使用这样的钩子定义自己的 ABC,然后定义一个将其子类化的泛型.

If you want to provide your own generics, you really want to study the typing module source code, specifically how GenericMeta and Generic are used to define the other Generic types like List; mostly such checks are delegated to abc.ABCMeta.__subclasshook__. You can define your own ABC with such a hook, then define a Generic that subclasses it.

这里的 GenericMeta 元类也赋予了 extra 关键字参数的含义.由于 typing 实现仍在不断变化,因此此类内部结构的文档仍然很少,模块仍然是 临时.extra 参数存储为 __extra__ 并用于 自定义__subclasshook__ 实现;对于 extra=list,它只是将 isinstance(something, List) 翻译成 isinstance(something, list).

It is the GenericMeta metaclass here that also gives the extra keyword argument meaning. Such internals are still sparsely documented because the typing implementation is still in flux, the module is still provisional. The extra argument is stored as __extra__ and is used in a custom __subclasshook__ implementation; for extra=list it simply comes down to translating isinstance(something, List) to isinstance(something, list).

请注意,对运行时检查的支持是故意限制的;静态类型检查器实际上不会运行这些钩子.请参阅 mypy 跟踪器中的 结构子类型讨论,以进一步讨论开发人员如何思考如何为复杂的自定义类提供更好的支持,这些类可能会或可能不会实现足够的方法以被视为映射序列或类似.

Note that support for run-time checks is deliberately limited; static type checkers will not actually run those hooks. See the structural subtyping discussion in the mypy tracker for further discussion on how the developers are thinking about how to provide better support for complex custom classes that may or may not implement enough methods to be deemed a mapping or a sequence or similar.

这篇关于isinstance 如何为 List 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆