TypeError: 'type' object is not subscriptable while using ->

问题描述 投票:-1回答:1

编写一个简单的类,它有一个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):)?

python python-3.x python-3.6 python-3.7
1个回答
0
投票

这是因为 list 需要 List. 看 此处. 您的代码索引了类型 list.

© www.soinside.com 2019 - 2024. All rights reserved.