8.局部变量/全局变量global/内嵌函数/闭包nonlocal

函数:有返回值
过程:无返回值
注解:在python中,只有函数(每个函数都有返回值),没有过程

>>> def hello():
    print("first")

>>> temp = hello()   #解释:虽然没有写明返回值,但是
first
>>> print(temp)    #打印出来的temp却有值None
None
>>> 

python可以返回多个值

>>> def back1():   #返回的值存放在stuple中
    return 1,2,'Curry'

>>> back1()
(1, 2, 'Curry')   #元祖stuple

>>> def back2():   #返回的值存放在list中
    return [1,2,'Curry']

>>> back2()
[1, 2, 'Curry']    #列表list

变量的作用域(局部变量,全局变量)

    存放位置:栈、全局变量区

    在函数里边定义的参数、变量,它们就是局部变量,出了这个函数,该变量就失效。
函数外部无法访问到这些局部变量。
    在函数外部定义的变量,全局变量。
    局部变量、全局变量如果同名,在函数内部,局部变量会覆盖全局变量的值。
>>> count = 5   #全局变量
>>> def MyFun():
    count = 10   #局部变量
    print(count)


>>> MyFun()
10
>>> print(count)
5

global关键字

告诉python编译器,把后面的变量变成全局变量:
>>> count = 5   #全局变量
>>> def MyFun():
    global count   #把count变量变成全局变量
    count = 10   #局部变量
    print(count)


>>> MyFun()
10
>>> print(count)
10

内嵌函数(内部函数):函数中有函数

1.
>>> def f2():   #先定义f2()函数
    print("f2()")


>>> def f1():  #在f1()函数中调用f2()函数
    f2()

>>> f1()
f2()

2.直接在f1()中定义并调用f2()函数
>>> def f1():
        def f2():  #在f1()函数中声明f2()函数
            print("f2 in f1")
        f2()  #在f1()函数中实现f2()函数

>>> f1()
f2 in f1
【注解】函数f2()是函数f1()的内嵌/内部函数,f2的作用域只在f1的内部生效,超出f1外,函数f2不可见。

闭 包
http://www.cnblogs.com/ma6174/archive/2013/04/15/3022548.html

闭包的条件:
- 函数funA中有内嵌函数funB
- 函数funA返回值是funB
- 函数funA中有自由变量temp
- 并且,自由变量temp在funB中被使用
那么:
    函数funA执行完成后,自由变量temp暂时不被回收,因为temp又被函数funB使用。

【例】
>>> def funA(temp):
        def funB(bb):
            return temp*bb
        return funB

>>> funA(10)(20)
200

>>> a = funA(100)
>>> a(20)
120

解释:funA被调用的时候产生一个闭包——funB,并且funB闭包持有funA中的自由变量temp,
    当funA函数的生命周期结束之后,temp变量依然存在,
因为temp被闭包funB使用了,暂时不会被回收。

【闭包的作用域问题】

>>> def f():
    a = 1     #② a是g()函数的“非全局的外部变量”
    def g():
        a = 2
        print a
    print a  #①
    return g

>>> f()()
1  
2

先执行①
在return g中执行②
>>> def f():
        count = 5   #5是g()函数"非全局的的外部变量",因此在g()函数中不认识count=5
        def g():
            count = count+100  #因此count在此时并没有初始值,所以没有初始值的count+100会报错
        return g
执行结果:报错
>>> f()()

Traceback (most recent call last):
  File "<pyshell#157>", line 1, in <module>
    f()()
  File "<pyshell#156>", line 4, in g
    count = count+100
UnboundLocalError: local variable 'count' referenced before assignment #count在引用前没有被赋值


【解决方案1】使用list列表:把"非全局变量的外部变量"放在list中,
然后再使用。

【因为】在python中,list中的元素是全局变量,不是局部变量。
>>> def f():
        count = [5,10,121,20]
        def g():
            count[0] = count[0]+100
            count[1] = count[1]+100
            print count[0],count[1]
        return g

>>> f()()
105 110
    上面把[5,10,121,20]放在list中,相当于变成了全局变量,
不会出现没有赋值的错误。

【解决方案2】python3中引出了关键字nonlocal 
def hellocounter (name):
    count=0 
    def counter():
        nonlocal count  #声明为全局变量
        count+=1
        print 'Hello,',name,',',str(count[0])+' access!'
    return counter

hello = hellocounter('ma6174')
hello()
hello()
hello() 
Python中怎么创建闭包?
在Python中创建一个闭包可以归结为以下三点:
- 闭包函数必须有内嵌函数
- 内嵌函数需要引用该嵌套函数上一级namespace中的变量
- 闭包函数必须返回内嵌函数

http://www.jb51.net/article/54498.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值