>>> [a, b] = 2, 3 >>> (a, b) = 2, 3 >>> {a, b} = 2, 3 File " ", line 1 SyntaxError: can't assign to literal
Python bug: Can assign [] = (), but not () = []
21–30 of 53 posts
Re: Python bug: Can assign [] = (), but not () = []
#22>>> [a, b] = 2, 3 >>> (a, b) = 2, 3 >>> {a, b} = 2, 3 File " ", line 1 SyntaxError: can't assign to literal
That could be because sets aren't ordered (even though it looks odd in this context).
>>> 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.
Re: Python bug: Can assign [] = (), but not () = []
#23Re: Python bug: Can assign [] = (), but not () = []
#24Re: Python bug: Can assign [] = (), but not () = []
#25Re: Python bug: Can assign [] = (), but not () = []
#26I can also do this, which is harmful. >>> True = False >>> False = True
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.
(https://gist.github.com/Jach/1208215 works in Python 3 too.)
Re: Python bug: Can assign [] = (), but not () = []
#27Earlier quoted context omitted.
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.
Only constant for certain values of "constant". ;) ( https://gist.github.com/Jach/1208215 works in Python 3 too.)
Re: Python bug: Can assign [] = (), but not () = []
#28Re: Python bug: Can assign [] = (), but not () = []
#29This is a fun numpy quirk: >>> 'x'*3.5 Traceback (most recent call last): File " ", line 1, in TypeError: can't multiply sequence by non-int of type 'float' >>> import numpy as np >>> print 'x'*np.float64(3.5) xxx
I use R predominantly, so forgive the lack of adequate Python-ese. Is this behavior because numpy overloads the multiplication operation with a string as string repetition and then implicitly casts the float64 down to an integer of 3? I'm curious why this behavior manifests. When I get a chance I'll test 'xyz'*np.float(3.5)
>>> "x" * 3
xxx
The only weird part (and it's not that weird IMO) is that numpy's "float" can be implicitly coerced to integer.Re: Python bug: Can assign [] = (), but not () = []
#30Earlier quoted context omitted.
Python 3 removed this feature unfortunately.
>>> (False, True) = (True, False) >>> 1 if False else 0 1 >>> bool(0) == False False
In[2]: (True, False) = (False, True)
In[3]: True
Out[3]: False
In[4]: 1 if False else 0
Out[4]: 1
In[5]: 'yes' if bool(0) == False else 'no'
Out[5]: 'no'
In[6]: bool(0) == False
Out[6]: False
What bothers me here is that when the interpreter says "False", it clearly means the opposite of (the new) False. So by reassigning True and False, I've actually caused inconsistent behavior in python, not just really-confusingly-named behavior. Suddenly False sometimes means one thing and sometimes means something else.