[Python-ideas] a in x or in y
Chris Angelico
rosuav at gmail.com
Thu Feb 13 21:17:27 CET 2014
On Fri, Feb 14, 2014 at 6:57 AM, Haoyi Li <haoyi.sg at gmail.com> wrote:
>> "somestring" in "wholesome" or in "string of words"
>>
>> is not the same as concatenating the two strings and checking if the
>> target string is in the result.
>
> That's true, but I strongly suspect we can get what we want via plain old
>
> "somestring" in "wholesome" | "string of words"
>
> syntax by overloading the | operator to return some data structure that does
> what we want with in.
That requires that everything implement some kind of pipe union type.
It's putting things backwards. Why should the str type have to be able
to make a union of itself and everything else? It's like the reasoning
behind having ''.join(seq) rather than seq.join(''). Compare:
>>> "foo" in "asdfoobar"
>>> "foo" in {"asd","foo","bar"}
>>> "foo" in "asdfoobar" or "foo" in {"asd","foo","bar"}
Okay, now how are you going to deduplicate the last line?
>>> "foo" in "asdfoobar" | {"asd","foo","bar"}
The only way this would work is by having some universal
"this-or-that" structure that doesn't care about its data types. And
once you have that, you may as well make it a language construct and
have lazy evaluation semantics:
>>> cmd not in forbiddens and in fetch_command_list()
If the forbiddens set is small and kept locally (as a blacklist should
be), tripping that check should be able to short-circuit the calling
of fetch_command_list, just as the fully written out version would:
>>> cmd not in forbiddens and cmd in fetch_command_list()
There's fundamentally no way to implement that with the | operator.
I like the proposal, largely for its parallel with the chained
comparison operators (1<x<3), but I would want to see it function like
the and/or operators, not like &/|.
ChrisA
More information about the Python-ideas
mailing list