> On Dec 31, 2019, at 07:30, 永田大和 <[email protected]> wrote:
>
> However, I think what you want to say is nice.
> So, I propose this way.
>
> if (1, 2, 3) or in [1, 4, 5]:
> code
Even knowing what you want this to mean, I have a very hard time reading it as
meaning that. To me, if `a or in b` means anything, it has to be something like
asking whether a is either truthy, or in b.
Meanwhile, in your reply to Chris’s suggestion to just use set operators:
> If one of the elements were instance of list(or unhashable type), that won't
> work.
That’s true (although you actually used a set in one of your own two examples,
which implies that this isn’t usually a problem, only occasionally).
But you could solve that with a different type that provides the set operators.
If you have values that are orderable, you can use a sorted set type (plenty of
them available on PyPI). If not, you can write a simple trivial type that
implements the set operators (in O(N*M) time—which sounds bad, but given that M
is already 1 for this idiom, it’s fine).
class TupleSet(tuple):
def __and__(self, other):
return type(self)(value for value in self if value in other)
And now:
if TupleSet((foo, bar, baz)) & foobar:
It doesn’t look quite as nice as:
if {foo, bar, baz) & foobar:
… but on the rare occasions where you have a type that’s neither hashable nor
orderable, it works.
The real problem is that the set operators don’t cover all possible uses of the
OP’s proposal. You can check whether any of the values are in foobar this way,
and you can check if any are equal to foobar by just using & {foobar} in place
of & foobar. But for other operators like > or in, there’s no way to translate
that repeated check to a set operation.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/3LGLAJBYOPFS4FQG4O5ZCESYOYTEZ7HZ/
Code of Conduct: http://python.org/psf/codeofconduct/