4 ms·
My pet peeve with python is the classic: return x, I have never wanted to declare a tuple without surrounding it with (). Too bad it's not a syntax error
by herge 12y ago
My pet peeve with python is the classic:
return x,
I have never wanted to declare a tuple without surrounding it with (). Too bad it's not a syntax error in python 3.
Also, as opposed to one of his examples, if you are using python 2.7, declare your exception blocks as:
except (FooException, BarException) as e:
It's forward compatible with python 3, it's easier to read and the syntax errors are clearer.
- icebraining 12y agoI have never wanted to declare a tuple without surrounding it with (). No? I do that occasionally, e.g.: x, y = 5, 6
- akx 12y agoDoing (x, y) = (5, 6) makes it clearer to "visually grep" that it's not a normal assignment though.
- icebraining 12y agoFair enough, that would be fine by me.
- rnnr 12y agoIt's the comma that determines an expression as a tuple, not the parentheseis.
- lucian1900 12y agoWhich is the source of the original problem, which I've also encountered myself.
- yeukhon 12y agoThat syntax reminds me of trying to do the following print x, print y But I generall find myself doing the , in the situation like (element1,) to create a new tuple.
- herge 12y agoNot all the time. x = , causes a syntax error, while x = (,) creates an empty tuple.
- dalke 12y agoUnder both Python 2.7 and Python 3.3 I get: >>> x=(,) File "<stdin>", line 1 x=(,) ^ SyntaxError: invalid syntax
- Spittie 12y agoFrom the Python docs (https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences https://docs.python.org/3/tutorial/datastructures.html#tuple...) > Empty tuples are constructed by an empty pair of parentheses; And it works fine >>> x = () >>> x () I guess Herge mean that? After all, his argument was that a tuple is not always defined by the comma.
- deckiedan 12y agoYou should be able to easily add an extra syntax rule to your editor or git pre-commit (/return.*,$/ type of thing if this bothers you too much...