Global & Local Variable in Python

Following code explain how 'global' works in the distinction of global variable and local variable.

 var = 'Global Variable'
print(var) def func1():
var = 'Local Variable'
print(var) def func2():
print(var) def func3():
global var
print (var)
var = 'Global Variable Changed in Function' func1()
func2()
func3() print(var)

Expected Result:

Global Variable

Local Variable

Global Variable

Global Variable

Global Variable Changed in Function

上一篇:Cannot refer to the non-final local variable user defined in an enclosing scope


下一篇:ASP.NET MVC学习之母版页和自定义控件的使用