gpt4 book ai didi

python - __annotations__ 类中条目的顺序是否与其定义一致?

转载 作者:行者123 更新时间:2023-12-04 03:50:47 25 4
gpt4 key购买 nike

假设我们有一个类定义:

class A:
z: 1
b: 2
Z: 3
g: 4
C: 5
A: 6
d: 7

标准 CPython 为 A.__annotations__ 提供了这个值:

{
'z': 1,
'b': 2,
'Z': 3,
'g': 4,
'C': 5,
'A': 6,
'd': 7
}

总是这样吗?规范对此有何规定?

最佳答案

PEP 526 中给出的变量注释语法规范陈述如下:

at the module or class level, if the item being annotated is a simplename, then it and the annotation will be stored in the __annotations__attribute of that module or class (mangled if private) as an orderedmapping from names to evaluated annotations.

因此默认似乎保证了有序映射。但是,PEP 还声明了以下内容:

__annotations__ is writable ... But attempting to update__annotations__ to something other than an ordered mapping mayresult in a TypeError ... (Note that the assignment to __annotations__... is accepted by the Python interpreter without questioning it - butthe subsequent type annotation expects it to be a MutableMapping ...)

因此,由于 MutableMapping 本身并不是有序的,因此第三方类的 __annotations__ 至少有可能是一个映射,它定义了一个任意的顺序:

from collections.abc import MutableMapping

class D(MutableMapping):
def __init__(self, *args):
self._d = {}

def __getitem__(self, key):
return self._d[key]

def __setitem__(self, key, value):
self._d[key] = value

def __delitem__(self, key):
del self._d[key]

def __iter__(self):
return reversed(self._d)

def __len__(self):
return len(self._d)


class A:
__annotations__ = D()
z: 1
b: 2
Z: 3
g: 4
C: 5
A: 6
d: 7

for item in A.__annotations__.items():
print(item)
('d', 7)
('A', 6)
('C', 5)
('g', 4)
('Z', 3)
('b', 2)
('z', 1)


关于python - __annotations__ 类中条目的顺序是否与其定义一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64444638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com