PyWart: The problem with "print"

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jun 3 16:31:37 EDT 2013


On Mon, 03 Jun 2013 15:09:48 -0400, Jason Swails wrote:

> But unlike RR, who suggests some elaborate interpreter-wide, ambiguous
> ignore-rule to squash out all of these functions, I'm simply suggesting
> that sometimes it's worth commenting-out debug print calls instead of
> 'just leaving them there because you won't notice the cost' :).

+1

Further to this idea, many command line apps have a "verbose" mode, where 
they print status messages as the app runs. Some of these include 
multiple levels, so you can tune just how many messages you get, commonly:

- critical messages only
- important or critical messages
- warnings, important or critical messages
- status, warnings, important or critical messages
- all of the above, plus debugging messages
- all of the above, plus even more debugging messages

Since this verbosity level is selectable at runtime, the code itself must 
include many, many calls to some equivalent to print, enough calls to 
print to cover the most verbose case, even though most of the time most 
such calls just return without printing.

This is a feature. And like all features, it has a cost. If (generic) 
your application does not benefit from verbose print statements scattered 
all throughout it, *don't put them in*. But if it will, then there is a 
certain amount of overhead to this feature. Deal with it, either by 
accepting the cost, or by writing more code that trades off complexity 
for efficiency. It's 2013, not 1975, and computers have more than 32K of 
RAM and the slowest CPU on the market is a million times faster than the 
ones that took us to the moon, and quite frankly I have no sympathy for 
the view that CPU cycles are so precious that we mustn't waste them. If 
that were the case, Python is the wrong language.



-- 
Steven



More information about the Python-list mailing list