[Tutor] Understanding what the code does behind the scenes

Dave Angel davea at ieee.org
Fri Oct 16 13:21:42 CEST 2009

Katt wrote:
>> <snip>
>> The textcolor() function returns None. so you need to keep it
>> out of your print statement:. This means you need to split your
>> print into multiple separate statements. (This will also be true
>> for the pywin32 version)
>> print "There are",
>> textcolor(4)
>> print apples_left,
>> textcolor(7)
>> print "left in the basket."
> The above code is very easy to understand when looking at it, but from 
> what I see of other programmers this would not be as pythonic.
Nothing wrong with that code.  It's readable, and everything is in one 
place.  But if you had a dozen items insstead of three, you'd get pretty 
tired of typing, and even more important, the next maintainer of the 
code will get tired of reading.  Having lots of similar code with a 
repeating pattern is a good clue that factoring might be called for..
>> The way I'd handle thus is to create a function which takes a
>> list of tuples as input, with each tuple containing the string
>> and its colour:
>> def colorPrint(strings):
>>    for string in strings:
>>         textcolor(string[1])
>>         print string[0],
> In the above function please let me know if I am correct in my 
> interpretation.
> The first line of course would be the defining of the function and 
> puting something in the parenthesis indicates that you will be passing 
> a value to this function.
> The second line says that for each string in the colorPrint statement 
> check to see what the color code is.
> The third line says that if it detects a ",#" to change it to a color 
> based on the textcolor function in the WConio module.
> The fourth line puzzles me though.  I think it says that when the 
> textcolor returns the zero that it doesn't print the None?  I am not 
> sure though.
> Could you let me know if I have the right idea?
> Thanks in advance,
> Katt
I'm in a good position to critique this function, since I recently 
posted a similarly confusing function.  Your questions tell me the 
function needs to be reworked, for readability.

My attempt at readability  (untested):

def colorPrint(color_items):       #1
    """Call this function to print one or more strings, each with
        a color number specified, to the console.  The argument
        is a list of items, where each item is
        a tuple consisting of a string and an integer color number,
        with the color number determining what color the string
        will be displayed in.  This function will not work with
        redirection, as the color logic only works with the Win32 
console."""   #2
    for text, color in color_items:      #3
        textcolor(color)          #4
        print text                    #5

I added line-number comments, just so we can comment on the lines 
without any confusion as to which ones we're talking about.

#1 - This is the function declaration line;  the function takes a single 
parameter, a list of color items.
#2 - This is a docstring, used to explain what the function is for, and 
briefly how to use it.  There are utilities to deal with docstrings, so 
it's better than just putting comments inline.
#3 - This is a loop construct, that iterates over the list that was 
passed as an argument to the function.  Notice the "text, color" 
syntax.  That automatically unpacks the tuple that each item of the list 
is supposed to be.
#4 - This is the win32 call that tells the console to change color.  
There's no ",#" logic or anything else.  It's simply taking a number and 
passing it to the console system.
#5 - This is an ordinary print, that goes to sys.stdout.  Notice that if 
it's redirected, the colors will still go to the console, so the 
redirected file will not have any color information in it.

It would be possible (and maybe even desirable) to write a different 
function to just take a single string, and parse it for some escape 
sequence to cause the color changes.  But that's not what this one does.


More information about the Tutor mailing list