[Python-ideas] a in x or in y

Chris Angelico rosuav at gmail.com
Thu Feb 13 12:27:19 CET 2014


On Thu, Feb 13, 2014 at 9:57 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> - given the restrictions on the parser, is this even possible? and

Generalizing the syntax, I'd see this as:

operand1 binary-op1 operand2 {and|or} binary-op2 operand3

which implicitly places the value (not the code) of operand1 between
and/or and binary-op2. Since all binary operators have higher
precedence than either and or or (all that's lower is lambda and
if/else), this notation is currently guaranteed to fail... except in
two cases, namely + and -, which exist in unary form as well. So if +
and - are excluded, it should be unambiguous. Whether or not the
parser can actually handle it is a question for someone who knows what
he's talking about, though :)

The way I see it, there should ideally be no syntactic rule against
using different operators on the two sides:

input("> ") in legalcommands and not in forbiddencommands
value > 20 or in {3,5,7,11}

even though it would allow insanity:

if 5 < int(input("Enter a number: ")) or < int(input("Greater than
five please: ")) or < int(input("Come on now! ")) or == print("Bah, I
give up."):
    print("Thank you.")

In this way, it's like chained comparisons:

1 < x <= 10

which don't mind mixing and matching.

ChrisA


More information about the Python-ideas mailing list