msg340084 - (view) |
Author: Dan Snider (bup) * |
Date: 2019-04-12 17:07 |
The rich comparison operators have an (far as I can tell, unnecessary) limitation compared to the other binary operators, being that the result of an unparenthesized comparison expression cannot be unpacked using the *iterable "unpack" operator (does that thing have an official name?) Here's a silly demonstration of what I'm talking about: >>> if 1: ... parser.expr("[*+-~d<<b-~+_]") # all binary/unary number ops work ... parser.expr("[*+-~d<=b-~+_]") ... <parser.st object at 0x011D4A58> Traceback (most recent call last): File "", line 3, in File "", line 1 [*+-~d<=b-~+_] ^ SyntaxError: invalid syntax >>> if 1: ... parser.expr("f(*+d<<-b)") ... parser.expr("f(*+d<=-b)") ... <parser.st object at 0x01205DD0> <parser.st object at 0x011D49C8> Because the limitation is not present for function calls, I suspect this is simply a "typo" that's gone unnoticed for years, due to nobody ever trying it. I'm hardly an expert on the parser and can barely read the grammar file so i might be totally wrong here. But then, what would be the difference between the expressions: [*a+b+c+d, *e-f] and [*a<b<c<d, *e<f], if a, b, c, d, e, and f are eg. instances of the class: >>> class S(list): __lt__ = list.__add__ |
|
|
msg340088 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2019-04-12 17:37 |
Sorry, I don't understand your demonstration. What's the mystery ``parser`` object with an ``expr`` method? What is it doing? Your comment says "all binary/unary number ops work" but I don't know what you mean by "work". Could you show some plain, vanilla Python code that demonstrates the problem? From your description here: > an unparenthesized comparison expression cannot be unpacked using > the *iterable "unpack" operator it sounds like you are talking about an operator precedence issue. Am I close? But I think you are wrong: py> print(*[] < [1, 2]) Traceback (most recent call last): File "", line 1, in TypeError: print() argument after * must be an iterable, not bool suggests that the < operator is evaluated before trying to unpack. Let's try with something else: class X: def __lt__(self, other): return [1, 2, 3] py> print(*X() < None) 1 2 3 Perhaps I have misunderstood something. |
|
|
msg340089 - (view) |
Author: SilentGhost (SilentGhost) *  |
Date: 2019-04-12 17:41 |
It seems to be parser module. https://docs.python.org/3/library/parser.html#creating-st-objects |
|
|
msg340549 - (view) |
Author: Eric V. Smith (eric.smith) *  |
Date: 2019-04-19 16:59 |
I assume the OP is using the stdlib parser module just to show what is a syntax error and what isn't. But most of the characters in the example strings aren't required, so it can be simplified. Here is a simpler case demonstrating what I think the OP is trying to say. This is not a syntax error: >>> [*0<<1] Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable (Ignore the type error, this shows that it's syntactically valid.) But this is a syntax error: >>> [*0<=1] File "", line 1 [*0<=1] ^ SyntaxError: invalid syntax Both of these are treated the same way, as not syntax errors: >>> f(*0==1) Traceback (most recent call last): File "", line 1, in NameError: name 'f' is not defined >>> f(*0<=1) Traceback (most recent call last): File "", line 1, in NameError: name 'f' is not defined Here's the above, using parser: >>> import parser >>> parser.expr("[*0<<1]") <parser.st object at 0x7fa18d93a430> >>> parser.expr("[*0<=1]") Traceback (most recent call last): File "", line 1, in File "", line 1 [*0<=1] ^ SyntaxError: invalid syntax >>> parser.expr("f(*0<<1)") <parser.st object at 0x7fa18d93a470> >>> parser.expr("f(*0<=1)") <parser.st object at 0x7fa18d93a410> I'm not sure this is worth fixing. Maybe if someone can find where in the grammar this is caused, and understands the side effects of fixing it, it could be addressed. But I expect it to be non-trivial. |
|
|