6 ms·
Python bug: Can assign [] = (), but not () = []
- amelius 11y agoThis is mostly harmless, as most people in that thread already noted.
- deleted 11y ago[deleted]
- what_nonono 11y agowhat? no no no. a) did you even read the link b) what does your statement even mean? note that (a,b,c) = (1,2,3) works perfectly.
- iaw 11y agoI 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 11y agoIf it was PHP they'd just write it down and claim it's documented behaviour.
- coldtea 11y agoNot 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?
- ngoldbaum 11y agoThis 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
- iaw 11y agoI 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)
- lmm 11y agoIt's "normal" in python for this to work with integers: >>> "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.
- shoyer 11y agoAs an occasional numpy contributor, I would call this a bug.
- ngoldbaum 11y agoGood point - I found it after it caused a really weird bug in a library I use. I'll open an issue on github...
- ngoldbaum 11y agoTurns 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'
- ronbeltran 11y agoI can also do this, which is harmful. >>> True = False >>> False = True
- rectangletangle 11y agoPython 3 removed this feature unfortunately.
- tdicola 11y agoUnfortunately? I'd love to know when changing false to mean true and vice-versa would make sense in any codebase.
- rectangletangle 11y ago>>> (False, True) = (True, False) >>> 1 if False else 0 1 >>> bool(0) == False False
- janzer 11y agoNot entirely sure of your point but... Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> (True, False) = (False, True) File "<stdin>", line 1 SyntaxError: can't assign to keyword
- exogen 11y agoBut, like the parent said, in Python 3: >>> (False, True) = (True, False) File "<stdin>", line 1 SyntaxError: can't assign to keyword
- thaumasiotes 11y agoIn[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.
- rectangletangle 11y ago>>> [a, b] = 2, 3 >>> (a, b) = 2, 3 >>> {a, b} = 2, 3 File "<interactive input>", line 1 SyntaxError: can't assign to literal
- rzimmerman 11y agoThat could be because sets aren't ordered (even though it looks odd in this context).
- rectangletangle 11y agoI 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 11y agoThe 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#assignment-statements https://docs.python.org/3/reference/simple_stmts.html#assign...
- rectangletangle 11y ago....__class__ # Python 3 only
- agumonkey 11y agoDave Beazley gave a talk showing this. See https://twitter.com/renfredxh/status/586653125970386945 https://twitter.com/renfredxh/status/586653125970386945
- tenfingers 11y agoUsing [] = to exhaust generators (as mentioned in the bug report) is actually pretty awesome in my mind.
- ixtli 11y ago> The starting point is recognizing that this has been around for very long time and is harmless.
- squigs25 11y ago() is not a tuple. (a,b) is a tuple or (a,) would also be a tuple.
- bpicolo 11y agoIt's an empty tuple. stuff = () print(type(stuff)) <class 'tuple'>
- sparaker 11y agoThis is what i see in javascript: [] = (); VM105:2 Uncaught ReferenceError: Invalid left-hand side in assignment () = []; VM114:2 Uncaught SyntaxError: Unexpected token )