I was surprised to find the following, in Python 3, the first two raise nothing:
>>> [] = () >>> () = () >>> {} = () File "<stdin>", line 1 SyntaxError: can't assign to literal
In Python 2.7, only the first one raises nothing:
>>> [] = () >>> () = () File "<stdin>", line 1 SyntaxError: can't assign to () >>> {} = () File "<stdin>", line 1 SyntaxError: can't assign to literal
What is going on here? Why are any of then not raising errors? And why was the () = ()
presumably added to be valid in Python 3?
*Note, you can replace the right hand side with any empty iterable (e.g. [] = set()
), I just choose an empty tuple for the illustration
So, what is the meaning of {} in JavaScript? In JavaScript, we use {} braces for creating an empty object. You can think of this as the basis for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array.
“+=”: This operator is combination of '+' and '=' operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. If initially value stored in a is 5. Then (a += 6) = 11.
The simple assignment operator ( = ) is used to assign a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables.
The value of an assignment expression is the value of the right-side operand. As a side effect, the = operator assigns the value on the right to the variable or property on the left so that future references to the variable or property evaluate to the value.
According to Issue23275, these are basically quirks causing no real harm but also no utility. Note that [] = ()
does not alter the list
literal:
>>> [] = () >>> type([]) <class 'list'>
[] = x
statements basically assert that x
is iterable and that x
is empty (although no-one would recommend using them this way), e.g.
>>> [] = (1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable >>> [] = (1,) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack
As John Y comments it is best to think of [] = ()
as not an assignment but a way of being consistent with Python's iterable unpacking syntax.
As ArrowCase comments, this syntax also extends to multiple assignments:
>>> a = [] = () >>> a ()
Looking at the CPython bytecode of the multiple assignment illustrates that this operations are similar to the normal iterable unpacking syntax, using the UNPACK_SEQUENCE
instruction:
>>> dis.dis('a = [] = ()') 1 0 BUILD_TUPLE 0 2 DUP_TOP 4 STORE_NAME 0 (a) 6 UNPACK_SEQUENCE 0 8 LOAD_CONST 0 (None) 10 RETURN_VALUE >>> dis.dis('[a, b] = (1, 2)') 1 0 LOAD_CONST 3 ((1, 2)) 2 UNPACK_SEQUENCE 2 4 STORE_NAME 0 (a) 6 STORE_NAME 1 (b) 8 LOAD_CONST 2 (None) 10 RETURN_VALUE
The same Issue23275 states that () = ()
was added as valid syntax to Python 3 for concordance. It was decided that removing [] = ()
would break code needlessly, since it causes no harm and fits with iterable unpacking logic. {} = ()
is still invalid because the unpacking syntax does not make sense in this context with braces.
In case anyone is wondering, syntax like list() = ()
is simply syntactically invalid, because you can never assign to function call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With