WANT: bad code in python (for refactoring example)

Steve D'Aprano steve+python at pearwood.info
Wed Feb 15 07:24:26 EST 2017


On Wed, 15 Feb 2017 09:49 pm, Antoon Pardon wrote:

> Op 15-02-17 om 07:28 schreef Steven D'Aprano:
[...]
>> Why not use sys.stdout.write directly? Or print? If I saw somebody using
>> this recipe in production code, in the way shown, I'd refactor it to just
>> use print. There's no advantage to re-inventing the wheel this way.
> 
> On reason to use this is for some easy "logging", you use echo to help
> in debugging and afterwards you can either define echo as an empty
> function or something easy to find to comment out.

In Python 3 you can always add 

def print(*args, **kw):
    pass

in your module to disable printing. But a better way, in my opinion, is to
use your editor to search for:

print(

and replace with:

#print(



But even better, once you get to the point of putting print calls in more
than two or three places in your code, you should probably invest the time
to learn how to use the logging module.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list