2

我完全不知道为什么我会收到错误

Traceback (most recent call last):
  File ".../minimal.py", line 8, in <module>
    obj = Clazz([0], [[0], [0]], [[0]])
  File ".../minimal.py", line 5, in __init__
    self.c = c
AttributeError: 'list' object has no attribute 'c'

从下面的一段代码

class Clazz(object):
    def __init__(self, a, b, c):
        self.a = a
        self,b = b
        self.c = c

if __name__ == "__main__":
    obj = Clazz([0], [[0], [0]], [0])

当我只通过

obj = Clazz([0], [0], [0])

我收到以下错误:

Traceback (most recent call last):
  File ".../minimal.py", line 8, in <module>
    obj = Clazz([0], [[0], [0]], [0])
  File ".../minimal.py", line 4, in __init__
    self,b = b
ValueError: need more than 1 value to unpack

我试图用谷歌搜索,但没有类似的问题。怎么会这样?这些错误的原因是什么?

4

1 回答 1

4

你需要一个点.

self.b = b

当你通过这个:

obj = Clazz([0], [[1], [2]], [0])  #   self, b = [[1], [2]]

您的代码分配[1]给 self 和[2]b,因此运行self.c会引发:

AttributeError: 'list' object has no attribute 'c' 

对于第二种情况:obj = Clazz([0], [1], [0])

self, b需要[1]有两个项目。所以,它提出了:

ValueError: need more than 1 value to unpack
于 2013-06-14T06:31:34.253 回答