编写一个简单的类,它有一个retuns[0,0]的方法。
class Solution:
def contain(self, nums: list[int], target: int) -> list[int]:
return [0,0]
Solution().contain(nums=[1,2,3,4], target=3)
Traceback (most recent call last):
line 1, in <module>
class Solution:
line 2, in Solution
def contain(self, nums: list[int], target: int) -> list[int]:
TypeError: 'type' object is not subscriptable
如果list[int], int只是占位符,为什么当我删除它们时,代码会运行(用def contain(self, nums, target):)?
这是因为 list
需要 List
. 看 此处. 您的代码索引了类型 list
.