小编Mum*_*m C的帖子

如何从输入模块导入List以识别Class中的类型List[int]?

运行 python3 代码时,出现以下错误:

'类型错误:'类型'对象不可下标'

当我尝试将__init__ class 参数设置为时,系统无法识别具有列表参数的输入模块List[int]

我已经typing.py通过安装了模块pip并尝试import typing

即使如此,系统也无法识别List[int]类定义中的类型。

下面是相同的代码,

from typing import List
class newcls:
    def __init__(self, a: int,arr: list[int]):
        self.a=a
        self.arr=arr
    def new1(self):
        print('a=',self.a)
        print(self.arr)
obj1=newcls(1,[1,2,3])
obj1.new1()
Run Code Online (Sandbox Code Playgroud)

我运行时收到的错误消息,

运行时错误为 def init (self, a: int,arr: list[int]):

类型错误:“类型”对象不可下标

types list importerror python-3.x

8
推荐指数
1
解决办法
3万
查看次数

标签 统计

importerror ×1

list ×1

python-3.x ×1

types ×1