How to actually write a program?

Alex Martelli aleaxit at yahoo.com
Tue Sep 7 13:30:48 CEST 2004


Max M <maxm at mxm.dk> wrote:

> Anna Martelli Ravenscroft wrote:
> 
> > One other tip: add lots of print statements. Print statements are your
> > friend. They can help you figure out what it's *actually* doing, (and more
> > importantly sometimes: what it's *NOT* doing) rather than just what you
> > hope it might be doing. For example, when you start on defining funcx(a),
> > you could start by having it just print what you gave it for arguments.
> > You can always comment out or remove the print statements afterwards.
> 
> If you intend to use print that way it might be more efficient to write
> a special test print function.
> 
> 
> def tp(text):
>      TEST = 1
>      if TEST:
>          print text
> 
> 
> That way you will only have to set TEST = 0 to avoid printing debug 
> print statements.

If you went that route, you would be well advised to make tp more
general:

def tp(*args):
    TEST = 1
    for arg in args: print arg,
    print

or possibly even more, including a **kwds which would let you drive such
aspects as suppressing newlines, rerouting output to another file
object, etc, etc.

In practice, I find that I disable and re-enable my print statements
much more selectively than such a function would support, and that when
all is said and done plain old print and adding/removing comment signs
as needed works best for me.


Alex



More information about the Python-list mailing list