[Tutor] help on newbie program

Magnus Lyckå magnus@thinkware.se
Wed May 28 22:17:03 2003


At 17:26 2003-05-28 -0600, Nick Jensen wrote:
>I've looked over the program in question time and time again, but I guess 
>my problem starts with the first line that is executed which is:
>
>run_test(get_questions())
>
>I know run_test function is run, but not sure what (get_questions()) is 
>doing for that. I think it is accessing the lists from the get_questions 
>function at the beginning, but that is really where I lose track of it.

I guessed so, and I gave you the answer in my previous
reply, but I might take this in some more detail.

Just as in mathematical expressions, where

a = (5 * (3 + 4))

needs to be worked out from the inside and out, and can
be split up like this:

temp = 3 + 4
a = 5 * temp

If you have

a = tan x
and
b = sin a
you can write this as
b = sin tan x

The same applies for function calls in programs.

a = fa()
b = fb(a)
c = fc(b)

can be written like

c = fc(fb(fa()))

It's not so strange is it? Since a = fa(), fb(a) must be
fb(fa()), and since c = fc(b) and b = fb(a) = fb(fa())
c = fc(fb(fa())). Right?

Note the diffence between fa and fa(). fa is a function object,
and fa() is a call to a function object that will return some
kind of object. In other words, you don't feed run_test with the
get_question-function, you feed run_test with the *output* from
get_question, i.e. the list of questions and answers.

The three line version above is probably clearer for the newbie, but
if we don't need to use the variables a and b any more, it might
save a little memory and possibly time (very little though) to avoid
those locals, and just pass the function resules directly to the
next function. It also makes the code more compact, and if this
means that your program fits in one page of paper instead of two,
it might make is easier to grasp the code.

In languages like C++, it's often not advisable to write like this
even if all readers of the code are experienced and understand it,
since it makes it more difficult to see exactly where compilation
errors really happen. It might also be useful to be able to see
the values of a and b in a debugger.

In Python, this is less of an issue. The tracebacks that occur on
exceptions, are usually so clear that we can see if the problem
is in fa, fb or fc, even if they are on the same row.

Also note that python treats all sorts of objects in a very unform way:

 >>> def myPrint(x):
...     print x
...
 >>> def double(i):
...     return i * 2
...
 >>> myPrint(double)
<function double at 0x014B9E98>
 >>> myPrint(double(4))
8
 >>> myPrint(double('Hello '))
Hello Hello
 >>> import math
 >>> myPrint(math)
<module 'math' (built-in)>
 >>> import random
 >>> myPrint(random)
<module 'random' from 'G:\Python22\lib\random.pyc'>
 >>> myPrint(myPrint)
<function myPrint at 0x014C3890>
 >>> myPrint(myPrint('Hello'))
Hello
None



--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program