Blog of Rob Galanakis (@robgalanakis)

Uh… a in b in c?

So I just came across some code like this in our Python 2.7 codebase:

if 'foo' in dirname in filename:

First, I was surprised this was a valid syntax. Second, I was surprised it didn’t work like

(a in b) in c

which would equal

True in c  # or False in c

but it doesn’t. It works like:

if (a in b) and (b in c)

This is similar to:

if a < b < c

This makes total sense and I guess has some uses, even if I think it reads funny, but it was unexpected to me so I figured I’d blog about it. I don’t know if this was intentional or not, or just happens because the interpreter/parser happens to unroll the operators in that way and everything “just works.”

Leave a Reply