[] =''之间有什么区别?和()=''?

时间:2017-01-05 16:37:27

标签: python

这个问题对我来说真的很混乱。窗帘后面究竟发生了什么?为什么后者没有工作?

>>> [] = ''
>>> () = ''
  File "<stdin>", line 1
SyntaxError: can't assign to ()

我的理论是右边必须有一个对象或可迭代对象,它的值/ s被分配给左侧容器中的变量数。这个想法基于:

>>> [a] = '1'
>>> [a,b] = '12'
>>> a
'1'
>>> b
'2'
>>> [a,b] = '123'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> [a] = ''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 0 values to unpack

在元组中也是如此:

>>> (a,) = '1'
>>> (a,b) = '12'
>>> a
'1'
>>> b
'2'
>>> (a,b) = '123'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> (a,) = ''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 0 values to unpack

但是有些东西我错过了因为失败了:

>>> [] = ''
>>> () = ''
  File "<stdin>", line 1
SyntaxError: can't assign to ()

发生了什么打破后者的任务?

0 个答案:

没有答案