#!/usr/bin/env python """ bug test I tried to keep this as simple as possible. """ ## as was recommended, I've reread the '4.1 Code blocks, execution frames, ##and namespaces' section. :) glob = 7 def test_01(f): a = 0 # the weard thing is that the above does not get into the globals of #the nested functions below. def nested_0(a=a): """a workaround""" #no questions here, all is as should be. print '\nnested_0:' print 'globals :', globals() print "eval('globals()') :", eval('globals()') print 'locals :', locals() print "eval('locals()') :", eval('locals()') print 'glob :', glob print "eval('glob') :", eval('glob') print 'a :', a print "eval('a') :", eval('a') def nested_1(): """the bug""" #The variable a should be global in relation to nested_1 function, #and thus, another way to explicitly 'import' it into the locals #would be to declare it global. global a print '\nnested_1:' print 'globals :', globals() print "eval('globals()') :", eval('globals()') print 'locals :', locals() print "eval('locals()') :", eval('locals()') print 'glob :', glob print "eval('glob') :", eval('glob') print 'a :', a print "eval('a') :", eval('a') def nested_2(): """the bug""" print '\nnested_2:' print 'globals :', globals() print "eval('globals()') :", eval('globals()') print 'locals :', locals() print "eval('locals()') :", eval('locals()') print 'glob :', glob print "eval('glob') :", eval('glob') print 'a :', a #(1) print "eval('a') :", eval('a') #(2) return eval(f)() ##now, here it is: # as defined in the section 4.1 of the 'Language Reference': # +=========================+=================================+======================+ # | Code block type | Global namespace | Local namespace | # +=========================+=================================+======================+ # | Function body | global n.s. of containing block | new n.s. | # +-------------------------+---------------------------------+----------------------+ # | String passed to eval() | global n.s. of caller | local n.s. of caller | # +=========================+=================================+======================+ # from the above it is clear that: # a. variable a is nether in the globals nor the locals of the # function nested_2. # b. if variable a is to be printed in (1), it would indicate that # it MUST be in a visible namespace, either local or global, # thus complying to scoping rules. # from a and b: # only one of the following MUST be correct: # - (1) should raise a 'NameError' exception. (3) # - (2) should be equivalent to (1), # and print tha value of a. (4) # # judging from the fact that in (1) variable a is printed, it MUST #be in a namespace, and (3) can not be correct. # # thus what we have is the 'gap' between the locals and the globals #of nested functions. (and this most likely is the problem.) #and it is unclear how and where in (1) the variable a is found, to #conflict with the Python Language Documentation. # if __name__ == '__main__': #the first is a typical demo of nested scopes. test_01('nested_0') #test_01('nested_1') test_01('nested_2') # vim:set ts=4 sw=4 :