码迷,mamicode.com
首页 > 编程语言 > 详细

Global & Local Variable in Python

时间:2014-10-22 12:37:34      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   sp   div   on   cti   

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

 

 

 1 var = Global Variable
 2 print(var)
 3 
 4 def func1():
 5     var = Local Variable
 6     print(var)
 7 
 8 def func2():
 9     print(var)
10 
11 def func3():
12     global var
13     print (var)
14     var = Global Variable Changed in Function
15 
16 func1()
17 func2()
18 func3()
19 
20 print(var)

 

 

Expected Result:

Global Variable

Local Variable

Global Variable

Global Variable

Global Variable Changed in Function

 

Global & Local Variable in Python

标签:style   blog   color   io   ar   sp   div   on   cti   

原文地址:http://www.cnblogs.com/Geek-GeNE/p/4042604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!