我是Python新手。我有一个方法:

def foo(self, list):
    length = len(list)

在其他情况下,我成功地调用了len(),但这里我得到:
TypeError: object of type 'type' has no len()

如何使Python相信传入的对象是一个列表?我错过了什么?

最佳答案

因为list是列表类型的名称。
使用其他名称。

def foo(self, lst):
    length = len(lst)

并确保您没有像这样呼叫foo
Foo.foo(list)

关于python - 为什么我在Python中找不到`len(list)`?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2408575/

10-14 17:37