[Tutor] Global name not found, though clearly in use

Emile van Sebille emile at fenx.com
Wed Jul 14 19:19:43 CEST 2010

On 7/14/2010 9:33 AM Corey Richardson said...
> Hmm..If I add a few debugging lines like that into my code, I get this:

The point was that statements in a class at class level (ie, not in 
defs) are executed sequentially and expect referenced variables to exist 
(ie, defined somewhere 'above' the current statement) -- there is no 
forward peeking going on.  Statements within def's (think 'deferred 
execution' if it helps) expect that the variables referred to will exist 
by the time the def is invoked.

Python executes code top to bottom.  def's create an executable object 
and defers execution until invoked.  Other statements execute when 
encountered.  The statements in your class that start

     top = tk.Tk()

execute in top-down sequence as they are outside of def's.  That's your 
problem.  It's unusual to have that done within a class definition and I 
expect it shouldn't be there.



More information about the Tutor mailing list