Re:Python bug: Can assign [] = (), but not () = []
by teddyh on Wednesday May 27, 2015 @03:04AM
(#9609782)
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.
[ Parent
| on HN ]
Re:Python bug: Can assign [] = (), but not () = []
by Jach on Wednesday May 27, 2015 @04:36AM
(#9610070)
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 () = []
by teddyh on Wednesday May 27, 2015 @04:46AM
(#9610092)
After “import ctypes”, all bets are off.
Re:Python bug: Can assign [] = (), but not () = []
by IsTom on Wednesday May 27, 2015 @06:20AM
(#9610385)
There are ways to mark memory pages as read-only. It's not even difficult.
Re:Python bug: Can assign [] = (), but not () = []
by rjaco31 on Wednesday May 27, 2015 @08:03AM
(#9610725)
Why would you do that? That code is working perfectly as intended, the user is allowed to do that
Re:Python bug: Can assign [] = (), but not () = []
by netheril96 on Wednesday May 27, 2015 @06:36AM
(#9610432)
Are there any codes dependent on True and False being assignable? Don't those codes deserve to die?
Re:Python bug: Can assign [] = (), but not () = []
by dalke on Wednesday May 27, 2015 @03:46PM
(#9613842)
Yes. There are codes written with backwards compatibility to versions of Python pre-2.3, which did not have True/False. They typically looked like this:
try:
True
except NameError:
True = 1==1
False = not True
Such code would likely be at least 10 years old. Given the transition to Python 3, it seems the general answer is "yes, they deserve to die."
Re:Python bug: Can assign [] = (), but not () = []
by TheLoneWolfling on Wednesday May 27, 2015 @04:30PM
(#9614108)
Couldn't that be kept compatible by allowing tautological assignments to True/False (True = (1==1), that sort of thing) but throwing an error on all other assignments?
Also, I don't see how that wouldn't be compatible with forbidding assignments to True/False. It'd never execute the except block.
Re:Python bug: Can assign [] = (), but not () = []
by dalke on Wednesday May 27, 2015 @05:48PM
(#9614608)
Yes, that specific case is possible with some sort of AST rewriting. But it's only one of several ways to introduce a True into the module or local namespace. Another might be:
from _compat import True, False, next, enumerate
This is allowed under Python 2, but not under Python 3. As you suggest, it could be made possible to allow an import like this so long as the actual imported value is indeed the True or False singleton. But it's a lot of work with little gain.While on the other hand, even in Python 2 it was a SyntaxError to say "None = None" or to import None. Extending that check to include True and False is much easier, and consistent with existing use.