Mum*_*m C 8 types list importerror python-3.x
运行 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]):
类型错误:“类型”对象不可下标
您的代码中有一个拼写错误。您的函数的签名__init__应为:
def __init__(self, a: int,arr: List[int]):
Run Code Online (Sandbox Code Playgroud)
(注意是大写List,不是内置类型list)
| 归档时间: |
|
| 查看次数: |
31132 次 |
| 最近记录: |