question of style

S

Steven D'Aprano

Why doesn't assemble_page properly handle the case where header, body,
and footer are all empty? That would let you eliminate the if. "Make
sure your code 'does nothing' gracefully" (Kernighan and Plauger).

You mean something like this?

def assemble_page(header, body, footer):
if header or body or footer:
do_lots_of_expensive_processing()
else:
do_nothing_gracefully()

Sure, why not?

Whatever way you do it, my point is that it is very useful to perform
branching tests on non-Boolean objects. Wherever you put the test, being
able to write:

if header or body or footer:

is a huge improvement over:

if (header != '') or (body != '') or (footer != ''):
 
P

Paul Rubin

Steven D'Aprano said:
def assemble_page(header, body, footer):
if header or body or footer:
do_lots_of_expensive_processing()
else:
do_nothing_gracefully()

Why should the processing be expensive if all three fields are empty?
if header or body or footer:
is a huge improvement over:
if (header != '') or (body != '') or (footer != ''):

Doesn't really seem any better. There's always something like

if any(p != '' for p in [header, body, footer, otherthing1, ...])

if the number of components gets larger.
 
S

Steven D'Aprano

Why should the processing be expensive if all three fields are empty?

Since I haven't specified an implementation for assemble_page, it could
be doing *anything*. Perhaps it has to talk to a remote database over a
slow link, perhaps it generates 300 lines of really inefficient HTML code
with no content, perhaps it sends a print job to a printer which then
warms up, cleans the print heads, and ejects a blank piece of paper.

Why does it matter? The example is about the `if` test, not about the
function assemble_page().
 
M

MRAB

koranthala said:
Actually, I felt that 0 not being considered False would be a better
option.
I had lot of instances where 0 is a valid value and always I had to
put checks specifically for that.
For me, None and False should be equal to False in if checks, every
thing else being considered True.

Can someone let me know why 0 is considered equal to False?
There should be real good reasons for it, only that I cannot think of
one.

Python did always have True and False.
 
E

Ethan Furman

Paul said:
Steven D'Aprano said:
def assemble_page(header, body, footer):
if header or body or footer:
do_lots_of_expensive_processing()
else:
do_nothing_gracefully()


Why should the processing be expensive if all three fields are empty?

if header or body or footer:
is a huge improvement over:
if (header != '') or (body != '') or (footer != ''):


Doesn't really seem any better. There's always something like

if any(p != '' for p in [header, body, footer, otherthing1, ...])

if the number of components gets larger.

Or if any(p for p in [header, body, footer, whatever, ...])

Which is even nicer! :) Particularly if header, et al, are some more
complicated objects which don't easily generate strings, but do easily
know whether they are Something or Nothing.

I suppose some of these discussions are based more on one's personal
requirements (using several languages, just a couple, or just Python),
and whether one wants to achieve Mastery of Python, or just be
Proficient. Neither choice is wrong, and wanting Mastery, of course,
does not mean not wanting improvements, etc. It does, however, require
a deep understanding of not just the mechanics, but the philosophy of
the language.

Mind you, I am nowhere near Mastery just yet (okay, okay, I have a
_long_ way to go! ;), but I love the philosophy of Python, it's
simplicity, and the *entirety* of the Zen.

~Ethan~
 
S

Steven D'Aprano

Python did always have True and False.

$ python1.5
Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat
4.1.2-27)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, AmsterdamTraceback (innermost last):
File "<stdin>", line 1, in ?
NameError: True
 
P

Paul Rubin

Steven D'Aprano said:
Since I haven't specified an implementation for assemble_page, it could
be doing *anything*. Perhaps it has to talk to a remote database over a
slow link, perhaps it generates 300 lines of really inefficient HTML code
with no content, perhaps it sends a print job to a printer which then
warms up, cleans the print heads, and ejects a blank piece of paper.

That sounds like a code smell to me. If the function is supposed to
print a piece of paper when any of those strings are nonempty, it
should also print one when they are all empty. Of the different
values a string can have, 'foo', 'bar', and '' are all perfectly good
values and should all be treated the same way.

If you want an absent string to work differently than a present one,
that's separate from empty vs nonempty, and overloading the empty
string to mean "absent" is another antipattern. Maybe what you really
want is a list of page constituents that are present:

page_parts = [header, body, footer]

If all the constituents are absent, then page_parts is the empty list,
so you would just test for page_parts being empty.
Why does it matter? The example is about the `if` test, not about the
function assemble_page().

Because of the notion that the code should "do nothing" gracefully.
The presence of that 'if' test is ungraceful, so its better to look
for ways to eliminate it than slickify it.
 
M

Matthew Barnett

Oops! I meant "didn't", of course.
$ python1.5
Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat
4.1.2-27)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, AmsterdamTraceback (innermost last):
File "<stdin>", line 1, in ?
NameError: True
 
P

Paul Rubin

Ethan Furman said:
Or if any(p for p in [header, body, footer, whatever, ...])

No need for the genexp:

if any([header, body, footer, whatever, ...])

But, you are using the built-in bool cast either way.
 
E

Ethan Furman

Paul said:
Ethan Furman said:
Or if any(p for p in [header, body, footer, whatever, ...])


No need for the genexp:

if any([header, body, footer, whatever, ...])

But, you are using the built-in bool cast either way.

Right you are -- and

if any([header, body, footer, whatever, ...])

sure looks nicer to me than your original code of

if any(p != '' for p in [header, body, footer, otherthing1, ...])


But there a are a lot of decisions and factors to writing these things
one way or the other. I'm just glad Python supports the Something vs.
Nothing model.

~Ethan~
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top