Tkinter WEIRDNESS or Python WEIRDNESS?

steve spambucket666au at yahoo.com.au
Sat Mar 12 18:46:57 EST 2005


Brian van den Broek <bvande at po-box.mcgill.ca> wrote in message news:<mailman.312.1110609301.1799.python-list at python.org>...
> steve said unto the world upon 2005-03-12 00:06:
> > In a nutshell, my problem is that I am getting this runtime error, and
> > I have no clue why.  Please bear in mind its my first python program:
> > 
> > "localvariable 'currentAbility' referenced before asignment"
> > 
> > in this function which is a callback for a button:
> > now the thing is, both lookingAtAbilities  and  currentAbility are
> > global variables.  In fact, they are both assigned at the same part of
> > the program, right at the start.  yet lookingAtAbilities  is
> > recognized as a global by this function, and currentAbility is not,
> > generating this runtime error.
> > 
> > here is the structure of the code:
> 
> <SNIP>
> 
> > This has me flumoxed
> > 
> > thanks if you can work it out,
> > 
> > Steve
> 
> Hi Steve,
> 
> While both lookingAtAbilities and currentAbility are names in the 
> global namespace, nextThing() doesn't know that ;-).
> 
> The first mention of if lookingAtAbilities in nextThing() is:
> .    if lookingAtAbilities == 1:
> This is a reference to the name, so the interpreter fetches 
> lookingAtAbilities value from the first namespace in which it finds 
> such a name. (The global namespace.)
> 
> By contrast, the first mention of currentAbility is:
> .        currentAbility = currentAbility + 1
> So, the interpreter `creates' (I am a bit fuzzy on the terminology and 
> not an accomplished Pythoner) a name in the function local namespace 
> and then looks to see what value to point the name at. When it sees it 
> should have currentAbility (a name without a value as yet) point to
> currentAbility + 1, it barfs.

Thanks to both of you, this has fixed the problem.  I wouldnt have
guessed that in a million years.

But it leads me to ask what is the use of something being in the
gloabl namespace if it cant be accessed globally?

I also find the continual need for objects to reference their own
variables by prefixing 'self' annoying.  It seems that Python is the
reverse of my assuptions.  whats the rationale for that?

> One way to fix it, is to put `global currentAbility' at the top of 
> your function definition. (Many frown on globals as it messes with the 
> namespaces, and gives bad mojo.)
> 
> Another would be to pass currentAbility in to your function as an 
> argument.
> 
> Yet another (and probably the best) would be to wrap your code into a 
> class and then reference self.currentAbility.
> 
> Still another (but one not so good for clarity or beauty) would be to 
> put a line like:
> temp = currentAbility # temp a name to reference global scope name
>                        # before assignment to follow.
> 
> Perhaps more expert voices will chime in and say better things. :-)
> 
> HTH,
> 
> Brian vdB



More information about the Python-list mailing list