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

Emile van Sebille emile at fenx.com
Wed Jul 14 17:54:47 CEST 2010


On 7/14/2010 8:31 AM Corey Richardson said...
> I was under the impression that when you define a function, it doesn't
> try to evaluate anything yet. If I had called the function before I
> defined the variable, I would understand, but I haven't.


The difference is in understanding what's executed and what's deferred.

Consider the following:

#-----Start File test.py-----
def test():
   print "in def test"

class Test:
   print "in class Test"
   def __init__(self):
     print "in class Test.__init__"

print "in __main__"

test()
t = Test()

#-----End File test.py-----

Output:

in class Test
in __main__
in def test
in class Test.__init__

-----------------

As you can see, the print statements at the class level are executing 
when encountered, but the prints in defs only when executed.

HTH,

Emile




More information about the Tutor mailing list