I refactored some pretty cruel code and came across the following pretty 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 could ever make any conceivable meaning.
I changed it to something like:
#!/usr/bin/env python2.7 if None in (opts.foo, opts.bar, opts.baz): # ...
I started the interpreter and actually tried the first construct ... it only works if the values ββare false and the last of these false values ββis None. (In other words, the CPython implementation returns the first true or last false value from the expression chain or ).
I still suspect that the correct code should use either the built-in elements any () or all () that were added 2.5 (2.7 is already required for this code). I'm not sure what the preferred / intended semantics are as I am just starting out on this project.
So, are there any cases where this source code would make sense?
python styles correctness
Jim dennis
source share