简体   繁体   English

如何通过python 3.5输入模块键入提示collections.OrderedDict

[英]how to type hint collections.OrderedDict via python 3.5 typing module

I want to use an OrderedDict where the key is a Enum and where the item is a certain class. 我想使用一个OrderedDict,其中键是一个枚举,并且该项是某个类。

How do I use the typing module to hint this? 我如何使用输入模块来暗示这一点? What is the analog to this hinted namedtuple:: 什么是这个暗示的namedtuple ::

Move = typing.NamedTuple('Move', [('actor', Actor), ('location', Location)])

As noted in a comment by AChampion, you can use MutableMapping : 如AChampion的评论中所述,您可以使用MutableMapping

class Actor(Enum):
    # ...Actor enum menbers...

class Location:
    # ...Location class body...

class MapActor2Location(OrderedDict, MutableMapping[Actor, Location]):
    pass

Addendum for people like me who haven't used the typing module before: note that the type definitions use indexing syntax ( [T] ) without parentheses. 像我这样以前没有使用过typing模块的人的附录:注意类型定义使用没有括号的索引语法( [T] )。 I initially tried something like this: 我最初尝试过这样的事情:

class MyMap(OrderedDict, MutableMapping([KT, VT])): pass

(Note the extraneous parentheses around [KT, VT] !) (注意[KT, VT]周围的无关括号!)

This gives what I consider a rather confusing error: 这给了我认为是一个相当混乱的错误:

TypeError: Can't instantiate abstract class MutableMapping with abstract methods __delitem__, __getitem__, __iter__, __len__, __setitem__

The question is about 3.5, but typing.OrderedDict was introduced in the python 3.7.2 . 问题是大约3.5,但是在Python 3.7.2中引入了 typing.OrderedDict So you can write: 所以你可以写:

from typing import OrderedDict
Movie = OrderedDict[Actor, Location]

or with backward compatibility workaround suggested by AChampion 或者使用AChampion建议的向后兼容性解决方法

try:
    from typing import OrderedDict
except ImportError:
    from typing import MutableMapping
    OrderedDict = MutableMapping
Movie = OrderedDict[Actor, Location]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 如何将 collections.OrderedDict 转换为 dataFrame - Python How to convert collections.OrderedDict to dataFrame Python - collections.OrderedDict() 未正确排序字典 - Python - collections.OrderedDict() is not ordering dictionary properly Python 3.7+ 规范是否保证 `collections.OrderedDict 不是 dict`? - Does the Python 3.7+ spec guarantee `collections.OrderedDict is not dict`? Django:'collections.OrderedDict' object 不可调用 - Django : 'collections.OrderedDict' object is not callable 使用collections.OrderedDict是不好的做法吗? - Is it bad practice to use collections.OrderedDict? 通过索引访问 collections.OrderedDict 中的项目 - Accessing items in an collections.OrderedDict by index “collections.OrderedDict”对象没有属性 - 'collections.OrderedDict' object has no attribute collections.OrderedDict 不适用于 json.dump() - collections.OrderedDict not working on json.dump() (1)TypeError: 不能连接类型为'的object<class 'collections.ordereddict'> '; 只有系列和 DataFrame 对象有效 (2)</class> - (1)TypeError: cannot concatenate object of type '<class 'collections.OrderedDict'>'; only Series and DataFrame objs are valid (2) Django AttributeError: &#39;collections.OrderedDict&#39; 对象没有属性 &#39;pk&#39; - Django AttributeError: 'collections.OrderedDict' object has no attribute 'pk'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM