Use of OR as branch control in FP Use of OR as branch control in FP python python

Use of OR as branch control in FP


Your interviewers must have had a "functional background" way back. It used to be common to write

(or (some-condition) (some-side-effect))

but in CL and in Scheme implementation that support it, it is much better written with unless. Same goes for and vs when.

So, to be more concrete -- it's not more functional (and in fact the common use of these things was for one-sided conditionals, which are not functional to begin with); there is no advantage (which becomes very obvious in these languages when you know that things are implemented as macros anyway -- for example, most or and and implementations expand to an if); and any possible use cases should use when and unless if you have them in your implementation, otherwise it's better to define them as macros than to not use them.

Oh, and you could use a combination of them instead of a two sided if, but that would be obfuscatingly ugly.


I'm not aware of any issues with the way this code will execute, but it is confusing to read for the uninitiated. In fact, this kind of syntax is like a Python anti-pattern: you can do it, but it is in no way Pythonic.


condition and true_branch or false_branch works in all languages that have short circuting logical operators. On the other hand it's not really a good idea to use in a language where values have a boolean value.

For example

zero = (1==0) and 0 or 1   # (1==0) -> False zero = (False and 0) or 1  # (False and X) -> Xzero = 0 or 1              # 0 is False in most languageszero = False or 1 zero = 1