condition and True or False

Peter Otten __peter__ at web.de
Sun May 2 14:13:42 EDT 2010


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".
> 
> I did a little code Googling and found a few other Python instances of
> this, but also many Lua instances.  I'm not that familiar with Lua, is
> this a practice that one who uses Lua frequently might carry over to
> Python, not realizing that the added "and True or False" is redundant?
> 
> Other theories?

If it were e. g.

def f():
    big_beast = list(range(10**100))
    return big_beast and True or False
x = f()

it would prevent that a big_beast reference becomes visible outside the 
function and allow for immediate release of its memory.

Peter



More information about the Python-list mailing list