Variable Scope in Functions : Variable Scope « Function « Python Tutorial






x = 10
def func() :
    x = 20
    print "in function func, x = ", x

def gfunc() :
    global x
    x = 20
print "in function gfunc, x = ", x

print "Initially, x = ", x
func()
print "After func, x = ", x
gfunc()
print "After gfunc, x = ", x








10.4.Variable Scope
10.4.1.Variable Scope and Namespaces
10.4.2.Shadow variable
10.4.3.globals() and locals()
10.4.4.create an instance to use the class simply as a namespace container
10.4.5.Variable name lookup in nested function
10.4.6.Local Names Are Detected Statically
10.4.7.Variable Scope in Functions