Pycharm: Type hint list of items(Pycharm:类型提示项目列表)
问题描述
我的问题不同,因为我在使用类型提示时犯了一个错误.
My question is different because I made a mistake using type hint.
我在 pycharm 中发现了一个奇怪的类型:
I found a weird type hinging in pycharm:
Example 是我自己的课程.但我想这不太重要,因为 IDE 抱怨 list 类型没有定义 __getitem__ 方法,这是不正确的.我想知道这是一个错误还是我以错误的方式使用它.
Example is my own class. But I guess this is less important because the IDE is complaining about list type does not define __getitem__ method which is no true. I'm wondering if it's a bug or I used it in a wrong way.
推荐答案
根据官方 PEP 来表示你应该使用 typing.List 的对象列表,而不是 list 内置的.
According to official PEP to denote list of objects you should use typing.List, not list builtin.
from typing import List
class Something:
pass
def f(seq: List[Something]): # no warning
for o in seq:
print(o)
2021 年 1 月更新:
Update January 2021:
请注意,内置泛型是在 Python 3.9 中实现的,如 中所述PEP585.
Please note that built-in generics were implemented in Python 3.9, as described in PEP585.
这篇关于Pycharm:类型提示项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Pycharm:类型提示项目列表
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
