[Tutor] help on scope

Charlie Clark Charlie Clark <charlie@begeistert.org>
Mon, 23 Jul 2001 12:11:54 +0200


>The easiest relief for the brain pain is to not cause it in the first place.
>;-)  Avoid scope problems by explicitly moving all variables in and out of
>functions.  For yours it would change it thus:
>
>def func(something,count):
>     print count
>     count += 1
>     return count

that's a good tip even it looks a little clumsy.

>If you have lots of variables that need to move through the function,
>organise them with classes.

I'm writing a little program that makes anagrams. Something I failed to do 
miserably in basic when I first dabbled in programming 20 years ago. I'm 
using count to help me check whether the algorithm is working properly. As I 
use a recursive function I need to initialise my counter outside the function 
but increment with each new word. Passing the counter as an argument removes 
problems of scope but feels wrong. I agree, however, that it is better than 
using global. Am I missing something?

Charlie
-- 
Charlie Clark
Helmholtzstr. 20
Düsseldorf
D- 40215
Tel: +49-211-938-5360
GSM: +49-178-463-6199
http://www.begeistert.org