[Tutor] Understanding what the code does behind the scenes

Eike Welk eike.welk at gmx.net
Fri Oct 16 13:53:43 CEST 2009


On Friday 16 October 2009, Katt wrote:
> > 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.

I think it's perfectly Pythonic, because it is easy to understand. I 
always try to make my programs look so simple that an 8 year old 
child could understand them. But I'm not always successful. 

One problem with a simple structure is, that a function might become 
long. And then it is difficult to understand because of its length. 
This is one reason why programming is an art. (Offcourse you can 
disagree and enjoy cleverly written Programs, that only the most 
smart people can understand. Programs, that are like the intricate 
gears of expensive Swiss wristwatches. But you should rather learn 
C++ in this case. :-))

I have an other idea for a color function. It cam be embedded in the 
print statement because it returns an empty string:


def color(color_num):
    '''Change text color and return empty string.'''
    textcolor(color_num)
    return ''

print "There are", color(4), apples_left, \
      color(7), "left in the basket."

#or

print "There are " + color(4) + str(apples_left) \
      + color(7) + " left in the basket."


Kind regards,
Eike.


More information about the Tutor mailing list