[Tutor] Out of range

Magnus Lycka magnus@thinkware.se
Tue Feb 4 15:55:04 2003


At 16:11 2003-02-04 -03-30, Adam Vardy wrote:
> >> If its defined in a function then you can't see it outside of
> >> that function. You need to use a debugger to stop execution inside
> >> the function and print it in the debugger(IDLE's debugger or
> >> pdb will both do that happily)
>
>Is there a command I can add in my program that will do this?   I
>presume that hoping a debugger to stop in the right place by chance is
>not quite effective.

No, you would set a breakpoint explicitly to the line where
you want it to stop.

>Oh and, what happens if the function runs a second time?  Are the
>variables like they were before?

What once was will be sadly forgotten, and it's all for the best.

This is the way we expect local variables in functions to
behave.

If you want the values to persist, you either use global
variables, as in:

x = 0

def increase_x(i):
     global x
     x = x + i

or, we use a class, and store our value as an attribute,
but that's a little more advanced.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se