True == 1 weirdness

Steven D'Aprano steve at pearwood.info
Wed Sep 16 19:24:58 CEST 2015


On Thu, 17 Sep 2015 12:03 am, Random832 wrote:

> Do chained "in" comparisons ever really make sense, even when they're
> all the same type?
> 
> I mean, I suppose 1 in (1, 2) in ((1, 2), (3, 4)) is technically true,
> but how useful is it really?


if fly in spider in rat in cat in dog in old_woman:
    print("I don't know why she swallowed the fly")


For a less whimsical example:


if word in line in text:
    print("word in line and line in text")


But I agree with Tim Chase: I wouldn't use it, even though it's legal.


-- 
Steven



More information about the Python-list mailing list