[Tutor] Testing print
boB Stepp
robertvstepp at gmail.com
Fri Sep 30 20:22:59 EDT 2016
On Fri, Sep 30, 2016 at 5:07 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Thu, Sep 29, 2016 at 09:24:51PM -0500, boB Stepp wrote:
>> Second, it seems that prints are often intermingled with the main
>> logic of a function and only serve to pass on a message to the user.
>
> Yeah, you normally shouldn't do that. Imagine if every time you called
> len(something), the len function was chatty and decided to print
> something. It would make it really hard to print anything where len() is
> involved as part of the calculation. Say I want to print a formatted
> table:
>
> -------- -------- --------
> COLUMN 1 COLUMN 2 COLUMN 3
> -------- -------- --------
> alpha second gamma
> letter
> -------- -------- --------
>
> but got something like:
>
>
> hi this is len I just wanted to let you know I'm working hard
> -------- hi this is len I just wanted to let you know I'm working hard
> -------- hi this is len I just wanted to let you know I'm working hard
> --------
> hi this is len I just wanted to let you know I'm working hard COLUMN
> 1 hi this is len oops something went wrong but that's okay I fixed it
> COLUMN 2 hi this is len I just wanted to let you know I'm working really
> hard you ought to be grateful okay COLUMN 3
> hi this is len I just wanted to let you know I'm working hard
> -------- hi this is len ...
<GRIN> You're a funny guy, Steve! I must confess, I have been very
cavalier in using prints in my functions. It was not until I was
trying to imagine last night how to test the example function I gave
that it truly dawned on me that this has been a baaaaad practice on my
part. This TDD practice has been making me think about my coding
efforts in ways that I never considered previously. Testing is a
very, very good thing!
Thanks, Steve!
--
boB
More information about the Tutor
mailing list