I am working on a project where a question came up about the following line:
a == "EQUAL" or a == "NOT EQUAL" or a == "LESS" or a == "GREATER"
a in ["EQUAL", "NOT EQUAL", "LESS", "GREATER"]
a in {"EQUAL", "NOT EQUAL", "LESS", "GREATER"}
...code is read much more often than it is written. The guidelines provided here are intended to improve the readability of code...
However, know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best.
I'd go with the set. It's much more readable. The string of
ors can be faster in some circumstances since the operator short circuits and there is no overhead of constructing the list of items each time but I don't think it's worth the readability sacrifice. Here is a quick and dirty benchmark. This is with Python 2.7
def t1(x): return (x == "Foo" or x == "Bar" or x == "Baz" or x == "Quux") def t2(x): return x in {"Foo", "Bar", "Baz", "Quux"} [2.7.9]>>> import timeit [2.7.9]>>> timeit.timeit(lambda : t1("Quux")) 0.22514700889587402 [2.7.9]>>> timeit.timeit(lambda : t1("Foo")) 0.18890380859375 [2.7.9]>>> timeit.timeit(lambda : t2("Quux")) 0.27969884872436523 [2.7.9]>>> timeit.timeit(lambda : t2("Foo")) 0.25904297828674316