Live data from Hacker News

Python bug: Can assign [] = (), but not () = []

bugs.python.org

41–50 of 53 posts

Re: Python bug: Can assign [] = (), but not () = []

#42

Earlier 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'

I get no such warning with 2.7.9 on linux.

Re: Python bug: Can assign [] = (), but not () = []

#43
post #40
post #37

Earlier 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?

Past experience and humor, mostly.

Re: Python bug: Can assign [] = (), but not () = []

#44
post #42

Earlier 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.

It would be a warning from numpy, not Python. That comes from numpy/core/src/multiarray/conversion_utils.c and was added 2013-04-13 , which would be for NumPy 1.8, I believe.

Re: Python bug: Can assign [] = (), but not () = []

#45
post #13

Earlier 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?

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 () = []

#46
post #45

Earlier 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."

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 () = []

#48
post #45

Earlier 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.

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.

Re: Python bug: Can assign [] = (), but not () = []

#49
post #43
post #40

Earlier 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.

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.

Re: Python bug: Can assign [] = (), but not () = []

#50

I can also do this, which is harmful. >>> True = False >>> False = True

Waaa! ... I just did that using 2.7 idle and it worked. This is new for me.

I got stuck for a while trying to back out. Finally figured the way to reverse is to use del(): del(True) del(False).

Post reply on HN