L (Local) 局部作用域 E (Enclosing) 閉包函數(shù)外的函數(shù)中 G (Global) 全局作用域 B (Built-in) 內(nèi)建作用域 以 L –> E –> G –>B 的規(guī)則查找,即:在局部找不到,便會(huì)去局部外的局部找(例如閉包),再找不到就會(huì)去全局找,再者去內(nèi)建中找 x = int(2.9) # 內(nèi)建作用域 g_count = 0 # 全局作用域 def outer(): o_count = 1 # 閉包函數(shù)外的函數(shù)中 def inner(): i_count = 2 # 局部作用域 ------------------------------------------------------ #!/usr/bin/python # -*- coding: utf-8 -*- total=0 def sum(arg1,arg2): total=arg1+arg2 print(total,end=" ") sum(12,21)#33 print("外部:",total)# 外部: 0 ------------------------------------------------------ 要么上面的這樣也可以 #!/usr/bin/python # -*- coding: utf-8 -*- def fun(a): a=a+1; print(a) fun(10)#11 ------------------------------------------------------ 分享知識(shí),分享快樂(lè)!希望中國(guó)站在編程之巔!
360圖書館館號(hào):rsgz002.360doc.com |
|