我完全不知道为什么会收到错误
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
我试图google它,但那里没有类似的问题。怎么会发生这种情况?这些错误的原因是什么?
答案 0 :(得分:4)
这里需要一个点.
:
self.b = b
当你通过时:
obj = Clazz([0], [[1], [2]], [0]) # self, b = [[1], [2]]
您的代码会将[1]
分配给自己,[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