> I am referring to Stack Diagrams
from
> http://www.ibiblio.org/obp/thinkCSpy/chap03.htm#11
>
> I did not understood this, please explain me with an example
The diagram he goives is an example but I'll try to reiterate slightly differently.
> http://www.ibiblio.org/obp/thinkCSpy/chap03.htm#11
>
> I did not understood this, please explain me with an example
The diagram he goives is an example but I'll try to reiterate slightly differently.
First the context:
def printTwice(bruce):
print bruce, bruce
print bruce, bruce
def catTwice(part1, part2):
cat = part1 + part2
printTwice(cat)
cat = part1 + part2
printTwice(cat)
>>> chant1
= "Pie Jesu domine, "
>>> chant2 = "Dona eis requiem."
>>> catTwice(chant1, chant2)
This is the code that he is discussing.
>>> chant2 = "Dona eis requiem."
>>> catTwice(chant1, chant2)
This is the code that he is discussing.
The stuff that is outside a function is in
the __main__ function,
which is a conceptual idea for the top
level of the program.
Working from the bottom of the code up we
see that in the
main function we have two variables:
chant1, chant 2 each
with values assigned. Thats the top box or
frame on the
diagram.
Main then calls catTwice which is the next
function up.
catTwice takes two arguments: part1,
part2
These arguments take the values of
chant1,chant2.
It also has a local variable called cat
which takes the
value of part1+part2
This is what is shown in the second frame
of the diagram.
catTwice then calls printTwice, our top
function.
printtwice takes a single argument
called(bizarrely!) bruce.
bruce takes the value of cat
This is shown in the third frame box of the
diagram.
The stack diagram is a debugging/diagnostic
tool used
in figuring out the state of the program at
any given time.
In practice they are rarely used, but the
concept often
is in the form of a variable table(you need
fixed width
font for this bit!):
chant1
chant2 part1 part2
cat bruce
'blah' 'blah'
- -
-
-
'blah' 'blah' 'blah' 'blah' - -
'blah' 'blah' 'blah' 'blah' 'blah
blah' -
'blah' 'blah' 'blah' 'blah' 'blah
blah' 'blah blah'
Even this is cumbersome except for numeric values and
I've only ever used it for really complex algorithms.
A simpler form of the stack diagram is presented by
Python when it gfenerates an error. The error message
includes a *stack trace* which shows the sequence of
functions. Thus if we introduce an error into
printTwice, we get the following stack trace:
>>> def printTwice(b):
... raise TypeError # create an error here
...
>>> def catTwice(p1,p2):
... c = p1 + p2
... printTwice(c)
...
>>> c1 = 'busy'
>>> c2 = 'bee'
>>> catTwice(c1,c2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in catTwice
File "<stdin>", line 2, in printTwice
TypeError
>>>
... raise TypeError # create an error here
...
>>> def catTwice(p1,p2):
... c = p1 + p2
... printTwice(c)
...
>>> c1 = 'busy'
>>> c2 = 'bee'
>>> catTwice(c1,c2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in catTwice
File "<stdin>", line 2, in printTwice
TypeError
>>>
Notice the three File lines are in fact the
frames from stack diagram. You can use the
Python debugger to examine the individual
frame variables as needed, so in practice
most folks use the debugger (or just insert
print statements!) to examine the stack
frames if needed rather than draw the diagram.
But that only works if you know what you
expect to see, thats where a stack diagram
or variable table comes in useful - to
figure out what you *expect* to see and
compare it with what the debugger says
*is* there.
HTH,
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor