condition and True or False

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun May 2 19:14:22 EDT 2010


On Sun, 02 May 2010 10:14:44 -0700, Paul McGuire wrote:

> While sifting through some code looking for old "x and y or z" code that
> might better be coded using "y if x else z", I came across this puzzler:
> 
>     x = <boolean expression> and True or False
> 
> What is "and True or False" adding to this picture?  The boolean
> expression part is already evaluating to a boolean, so I don't
> understand why a code author would feel compelled to beat this one over
> the head with the additional "and True or False".

If <boolean expression> is already an actual bool, then I can only 
imagine that the author is simply struggling with the concept, in the 
same way that some people write:

if <boolean expression> == True:
    ...


If it is any arbitrary object, then "x and True or False" is just an 
obfuscated way of writing "bool(x)". Perhaps their code predates the 
introduction of bools, and they have defined global constants True and 
False but not bool. Then they removed the True and False bindings as no 
longer necessary, but neglected to replace the obfuscated conversion.



-- 
Steven



More information about the Python-list mailing list