阅读目录html
楔子python
假若有一个函数,实现返回两个数中的较大值:网络
def my_max(x,y): m = x if x>y else y return m
bigger = my_max(10,20)
print(bigger)
以前是否是我告诉大家要把结果return回来大家就照作了?但是大家有没有想过,咱们为何要把结果返回?若是咱们不返回m,直接在程序中打印,行不行?闭包
来看结果:app
>>> def my_max(x,y): ... m = x if x>y else y ... >>> my_max(10,20) >>> print(m) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'm' is not defined
报错了!错误是“name 'm' is not defined”。变量m没有被定义。。。为啥?我明明定义了呀!less
在这里咱们首先回忆一下python代码运行的时候遇到函数是怎么作的。ide
从python解释器开始执行以后,就在内存中开辟了一个空间函数
每当遇到一个变量的时候,就把变量名和值之间的对应关系记录下来。post
可是当遇到函数定义的时候解释器只是象征性的将函数名读入内存,表示知道这个函数的存在了,至于函数内部的变量和逻辑解释器根本不关心。ui
等执行到函数调用的时候,python解释器会再开辟一块内存来存储这个函数里的内容,这个时候,才关注函数里面有哪些变量,而函数中的变量会存储在新开辟出来的内存中。函数中的变量只能在函数的内部使用,而且会随着函数执行完毕,这块内存中的全部内容也会被清空。
咱们给这个“存放名字与值的关系”的空间起了一个名字——叫作命名空间
代码在运行伊始,建立的存储“变量名与值的关系”的空间叫作全局命名空间,在函数的运行中开辟的临时的空间叫作局部命名空间
命名空间和做用域
命名空间的本质:存放名字与值的绑定关系

>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
在python之禅中提到过:命名空间是一种绝妙的理念,让咱们尽情的使用发挥吧!
命名空间一共分为三种:
全局命名空间
局部命名空间
内置命名空间
*内置命名空间中存放了python解释器为咱们提供的名字:input,print,str,list,tuple...它们都是咱们熟悉的,拿过来就能够用的方法。
三种命名空间之间的加载与取值顺序:
加载顺序:内置命名空间(程序运行前加载)->全局命名空间(程序运行中:从上到下加载)->局部命名空间(程序运行中:调用时才加载)
取值:
在局部调用:局部命名空间->全局命名空间->内置命名空间

x = 1 def f(x): print(x) print(10)
在全局调用:全局命名空间->内置命名空间

x = 1 def f(x): print(x) f(10) print(x)

print(max)
做用域
做用域就是做用范围,按照生效范围能够分为全局做用域和局部做用域。
全局做用域:包含内置名称空间、全局名称空间,在整个文件的任意位置都能被引用、全局有效
局部做用域:局部名称空间,只能在局部范围内生效
globals和locals方法

print(globals()) print(locals())

def func(): a = 12 b = 20 print(locals()) print(globals()) func()
global关键字

a = 10 def func(): global a a = 20 print(a) func() print(a)
函数的嵌套和做用域链
函数的嵌套调用

def max2(x,y): m = x if x>y else y return m def max4(a,b,c,d): res1 = max2(a,b) res2 = max2(res1,c) res3 = max2(res2,d) return res3 # max4(23,-7,31,11)
函数的嵌套定义

def f1(): print("in f1") def f2(): print("in f2") f2() f1()

def f1(): def f2(): def f3(): print("in f3") print("in f2") f3() print("in f1") f2() f1()
函数的做用域链

def f1(): a = 1 def f2(): print(a) f2() f1()

def f1(): a = 1 def f2(): def f3(): print(a) f3() f2() f1()

def f1(): a = 1 def f2(): a = 2 f2() print('a in f1 : ',a) f1()
nonlocal关键字
# 1.外部必须有这个变量
# 2.在内部函数声明nonlocal变量以前不能再出现同名变量
# 3.内部修改这个变量若是想在外部有这个变量的第一层函数中生效

def f1(): a = 1 def f2(): nonlocal a a = 2 f2() print('a in f1 : ',a) f1()
函数名的本质
函数名本质上就是函数的内存地址
1.能够被引用

def func(): print('in func') f = func print(f)
2.能够被看成容器类型的元素

def f1(): print('f1') def f2(): print('f2') def f3(): print('f3') l = [f1,f2,f3] d = {'f1':f1,'f2':f2,'f3':f3} #调用 l[0]() d['f2']()
3.能够看成函数的参数和返回值
*不明白?那就记住一句话,就当普通变量用

第一类对象(first-class object)指 1.可在运行期建立 2.可用做函数参数或返回值 3.可存入变量的实体。
闭包
def func(): name = 'eva' def inner(): print(name)
闭包函数:
内部函数包含对外部做用域而非全剧做用域名字的引用,该内部函数称为闭包函数
#函数内部定义的函数称为内部函数
因为有了做用域的关系,咱们就不能拿到函数内部的变量和函数了。若是咱们就是想拿怎么办呢?返回呀!
咱们都知道函数内的变量咱们要想在函数外部用,能够直接返回这个变量,那么若是咱们想在函数外部调用函数内部的函数呢?
是否是直接就把这个函数的名字返回就行了?
这才是闭包函数最经常使用的用法
def func(): name = 'eva' def inner(): print(name) return inner f = func() f()
判断闭包函数的方法__closure__
#输出的__closure__有cell元素 :是闭包函数 def func(): name = 'eva' def inner(): print(name) print(inner.__closure__) return inner f = func() f() #输出的__closure__为None :不是闭包函数 name = 'egon' def func2(): def inner(): print(name) print(inner.__closure__) return inner f2 = func2() f2()

def wrapper(): money = 1000 def func(): name = 'eva' def inner(): print(name,money) return inner return func f = wrapper() i = f() i()

from urllib.request import urlopen def index(): url = "http://www.xiaohua100.cn/index.html" def get(): return urlopen(url).read() return get xiaohua = index() content = xiaohua() print(content)
本章小结