condition and True or False

P

Paul McGuire

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?

-- Paul
 
P

Peter Otten

Paul said:
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
 
V

Vito 'ZeD' De Tullio

Peter said:
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.

what's wrong in bool(big_beast)?
 
D

David Robinow

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?

-- Paul
True and False were added in Python 2.2.1 (PEP 285)
Perhaps this was a silly way to ensure that the user wouldn't try to
run it in earlier versions.
 
C

Carl Banks

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?

The person who wrote it was a programmer who fancied himself to be
more clever than he really was.

Convenient though they may be, people always abuse (and often misuse)
these boolean shortcuts, a big reason why I don't like them. You
never see crap like this in Java.


Carl Banks
 
S

Steven D'Aprano

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.
 
P

Patrick Maupin

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?

-- Paul

I think it's idiomatic -- that it was written by someone who was deep
in thought about actually getting something accomplished, and not
thinking at the level of the details.

As you're actively *looking* for "x and y or z" I'm sure you'll agree
that we've probably all written lots of stuff like:

x = <expression> and 'some_prefix' or ''
x = <expression> and 42 or 0
x = <expression> and ['Hi, mom!'] or []

When you're in this mode of expression, the only thing that would
really trip you up and make it "wrong" is if 'some_prefix' or 42 or
['Hi, mom!'] evaluated to False, and then you get the '', 0, or [] you
didn't really want. So I know that, to the extent I was thinking
deeply about the low level of what I was writing, my mental energy
would be going towards making sure that the second sub-expression
evaluated to true.

So, if you're mentally operating in this mode, and you want True or
False, and you forget, don't think about, or are not cognizant of the
fact that bool() is available, it's pretty obvious what the result is
going to be. I've probably done it myself a few times, although I
would probably have to be *really* lost in thought in order to do it
when the underlying sub-expression was boolean to start with.

Regards,
Pat
 
J

John Machin

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.

Or perhaps they are maintaining code that must run on any 2.X. True
and False would be set up conditional on Python version. Writing
"expression and True or False" avoids a function call.
 
L

Lawrence D'Oliveiro

In message
Paul said:
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

I suspect the “boolean expression†isn’t actually evaluating to a Boolean at
all, this is simply a long-winded way of converting values that Python
treats as “false†(e.g. None, 0, what else?) to actual False, and everything
else to True.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top