Odd truth result with in and ==

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Wed Nov 21 19:02:51 EST 2018


On 11/21/18 6:45 PM, Ian Kelly wrote:
 > On Wed, Nov 21, 2018 at 2:53 PM Serhiy Storchaka 
<storchaka at gmail.com> wrote:
 >>
 >> 21.11.18 22:17, Cameron Simpson пише:
 >>> Can someone show me a real world, or failing that - sane looking,
 >>> chained comparison using "in"?
 >>
 >> s[0] == s[-1] in '\'"'
 >>
 >> Tests that string s starts and ends with a single or double quote.
 >
 > Clever, but too esoteric to use in practice. I'd rather make the
 > chaining explicit.
 >
 >> It can be also used with sets:
 >>
 >> elem in set1 <= set2
 >
 > I like this one better.
LOL: I had exactly the opposite reaction.
The first one expresses exactly the idea that the first and last
elements of the string match and are quotation characters, as if I'd
written:
 (s[0] == s[-1]) and (s[0] in '\'"')
(Arguably too esoteric to be used in practice, *unless* profiling
indicates a necessary (before) and noticable (after) speed improvement.)
But the second one has to do an expensive subset operation. If I think
"is elem in both sets," then I'd never write:
 (elem in set1) and (set1 <= set2)


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /