I've been refactoring some rather crufty code and came across the following rather odd construct:
#!/usr/bin/env python2.7
# ...
if (opts.foo or opts.bar or opts.baz) is None:
# (actual option names changed to protect the guilty)
sys.stderr.write("Some error messages that these are required arguments")
... and I was wondering if this would ever make any conceivable sense.
I changed it to something like:
#!/usr/bin/env python2.7
if None in (opts.foo, opts.bar, opts.baz):
# ...
I did fire up an interpreter and actually try the first construct ... it only seems to work if the values are all false and the last of these false values is None. (In other words CPython's implementation seems to return the first true or last false value from a chain of or expressions).
I still suspect that the proper code should use either the any() or all() built-ins which were added 2.5 (the code in question already requires 2.7). I'm not yet sure which are the preferred/intended semantics as I'm just starting on this project.
So is there any case where this original code would make sense?