

Python bug: Can assign [] = (), but not () = [] - axit
https://bugs.python.org/issue23275

======
iaw
I was very impressed with the collective thought process shown in the bug
thread. It was quite constructive and I think demonstrative of why python has
been such a successful project.

~~~
echeese
If it was PHP they'd just write it down and claim it's documented behaviour.

~~~
coldtea
Not consistent with my recollection of PHP since version 5, where they have
fixed tons of old inconsistencies from the parser to the API level.

Perhaps based on older versions of PHP or merely uninformed snark?

~~~
echeese
Past experience and humor, mostly.

~~~
coldtea
It used to be quite bad, but it's getting better in a good rate. Of course
they have to keep bad stuff around for backwards compatibility, but unlike say
Java they wasn't afraid to break too if it was for something smaller.

------
ngoldbaum
This is a fun numpy quirk:

    
    
        >>> 'x'*3.5
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: can't multiply sequence by non-int of type 'float'
        >>> import numpy as np 
        >>> print 'x'*np.float64(3.5) 
        xxx

~~~
shoyer
As an occasional numpy contributor, I would call this a bug.

~~~
p1mrx
Yes, the expected output is xx›

~~~
thezilch

      xxx›

~~~
teddyh
xxxₓ

~~~
pluma
xxxv, actually.

(it makes sense if you know why Roman numerals look the way they do)

------
agumonkey
Dave Beazley gave a talk showing this. See
[https://twitter.com/renfredxh/status/586653125970386945](https://twitter.com/renfredxh/status/586653125970386945)

------
amelius
This is mostly harmless, as most people in that thread already noted.

------
tenfingers
Using [] = to exhaust generators (as mentioned in the bug report) is actually
pretty awesome in my mind.

------
rectangletangle
>>> [a, b] = 2, 3

>>> (a, b) = 2, 3

>>> {a, b} = 2, 3

    
    
      File "<interactive input>", line 1
    

SyntaxError: can't assign to literal

~~~
rzimmerman
That could be because sets aren't ordered (even though it looks odd in this
context).

~~~
rectangletangle
I thought that at first too, but even then the exception seems spurious. It
would be more inline with existing Python behavior to evaluate it as
undefined/indeterminate assignment order (though this would be pretty
useless).

>>> a, b, c, d = {1, 2, 3, 'a'}

>>> a

'a'

Regardless, it shouldn't be surprising when writing insane code, that the
language starts acting insane. Python as a whole is pretty decent when it
comes to handling syntactic edge cases.

~~~
netmare
The tricky part is that the [a,b] and (a,b) in your original examples are not
list/tuple literals, but a target_list according to Python's grammar [0],
which can be optionally enclosed in parentheses or brackets. To be honest, I
didn't know the latter were allowed, even though I've been using Python for
years. IMHO, they should have just allowed parentheses. It's like the
parentheses in function calls--not really a tuple, but syntactic grouping. Why
have more that one way to do the same thing?

[0]:
[https://docs.python.org/3/reference/simple_stmts.html#assign...](https://docs.python.org/3/reference/simple_stmts.html#assignment-
statements)

------
sparaker
This is what i see in javascript:

[] = (); VM105:2 Uncaught ReferenceError: Invalid left-hand side in assignment

() = []; VM114:2 Uncaught SyntaxError: Unexpected token )

------
ixtli
> The starting point is recognizing that this has been around for very long
> time and is harmless.

------
ronbeltran
I can also do this, which is harmful.

    
    
        >>> True = False
        >>> False = True

~~~
teddyh
Only in Python 2, since that is a backward-compatibility feature. (“False” and
“True” used to be _variables_ containing 0 and 1 before Python had true
booleans, many ages ago.) Python 3 makes “True” and “False” be constant
values, like “1” or “2”, like they should be.

~~~
Jach
Only constant for certain values of "constant". ;)

([https://gist.github.com/Jach/1208215](https://gist.github.com/Jach/1208215)
works in Python 3 too.)

~~~
teddyh
After “import ctypes”, all bets are off.

~~~
IsTom
There are ways to mark memory pages as read-only. It's not even difficult.

~~~
rjaco31
Why would you do that? That code is working perfectly as intended, the user is
allowed to do that

------
squigs25
() is not a tuple. (a,b) is a tuple or (a,) would also be a tuple.

~~~
bpicolo
It's an empty tuple.

stuff = ()

print(type(stuff))

<class 'tuple'>

------
rectangletangle
....__class__ # Python 3 only

