Python bug: Can assign [] = (), but not () = []
41–50 of 53 posts
Re: Python bug: Can assign [] = (), but not () = []
#42Earlier quoted context omitted.
Good point - I found it after it caused a really weird bug in a library I use. I'll open an issue on github...
Turns out it's deprecated: >>> import warnings >>> warnings.simplefilter('always') >>> 'x'*np.float64(3.5) __main__:1: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future 'xxx'
Re: Python bug: Can assign [] = (), but not () = []
#43Earlier quoted context omitted.
If it was PHP they'd just write it down and claim it's documented behaviour.
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?
Re: Python bug: Can assign [] = (), but not () = []
#44Earlier quoted context omitted.
Turns out it's deprecated: >>> import warnings >>> warnings.simplefilter('always') >>> 'x'*np.float64(3.5) __main__:1: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future 'xxx'
I get no such warning with 2.7.9 on linux.
Re: Python bug: Can assign [] = (), but not () = []
#45Earlier 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.
Are there any codes dependent on True and False being assignable? Don't those codes deserve to die?
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 () = []
#46Earlier quoted context omitted.
Are there any codes dependent on True and False being assignable? Don't those codes deserve to die?
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."
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 () = []
#47This 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
Re: Python bug: Can assign [] = (), but not () = []
#48Earlier quoted context omitted.
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."
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.
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.
Re: Python bug: Can assign [] = (), but not () = []
#49Earlier quoted context omitted.
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?
Past experience and humor, mostly.
Re: Python bug: Can assign [] = (), but not () = []
#50I can also do this, which is harmful. >>> True = False >>> False = True
I got stuck for a while trying to back out. Finally figured the way to reverse is to use del(): del(True) del(False).