L (Local) 局部作用域
E (Enclosing) 闭包函数外的函数中
G (Global) 全局作用域
B (Built-in) 内建作用域
以 L –> E –> G –>B 的规则查找,即:在局部找不到,便会去局部外的局部找(例如闭包),再找不到就会去全局找,再者去内建中找
x = int(2.9) # 内建作用域
g_count = 0 # 全局作用域
def outer():
o_count = 1 # 闭包函数外的函数中
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
------------------------------------------------------
分享知识,分享快乐!希望中国站在编程之巅!
----融水公子
公众微信号:rsgz520
360图书馆馆号:rsgz002.360doc.com
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。