[TimLinux] Python nonlocal和global的作用

1. 执行代码

以下实例都是通过执行以下代码,需要把以下执行代码放在后面实例代码的后面。

a = outer_func()print("call a()")
a()
a()
a()

b = outer_func()print("call b()")
b()
b()
b()

2. 未使用nonlocal

def outer_func():
    count = 3
    def inner_func():
        count += 1
        print("count", count)
    return inner_func

#output>>>
#    count += 1
#UnboundLocalError: local variable ‘count‘ referenced before assignment

3. 使用nonlocal

def outer_func():
    count = 3
    def inner_func():
        nonlocal count
        count += 1
        print("count", count)
    return inner_func

# output>>>
# call a()
# count 4
# count 5
# count 6

# call b()
# count 4
# count 5
# count 6

4. 使用global (出错)

def outer_func():
    count = 3
    def inner_func():
        global count
        count += 1
        print("count", count)
    return inner_func

# output>>>
#     count += 1
# NameError: name ‘count‘ is not defined

5. 使用global (成功)

count = 3
def outer_func():
    def inner_func():
        global count
        count += 1
        print("count", count)
    return inner_func

# output>>>
# call a()
# count 4
# count 5
# count 6

# call b()
# count 7
# count 8
# count 9

原文地址:https://www.cnblogs.com/timlinux/p/9193222.html

时间: 2024-08-09 17:12:31

[TimLinux] Python nonlocal和global的作用的相关文章

FAQ: Python中if __name__ == '__main__':作用

#hello.pydef sayHello(): str="hello" print(str); if __name__ == "__main__": print ('This is main of module "hello.py"') sayHello() python作为一种脚本语言,我们用python写的各个module都可以包含以上那么一个类似c中的main函数,只不过python中的这种__main__与c中有一些区别,主要体现在:

浅析python 中__name__ = '__main__' 的作用

很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = '__main__' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and executable" 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. __name__ 是当前模块名,当模块

谈谈python 中__name__ = '__main__' 的作用

position:static(静态定位) 当position属性定义为static时,可以将元素定义为静态位置,所谓静态位置就是各个元素在HTML文档流中应有的位置 podisition定位问题.所以当没有定义position属性时,并不说明该元素没有自己的位置,它会遵循默认显示为静态位置,在静态定位状态下无法通过坐标值(top,left,right,bottom)来改变它的位置. position:absolute(绝对定位) 当position属性定义为absolute时,元素会脱离文档流

python 中__name__ = '__main__' 的作用

有句话经典的概括了这段代码的意义: "Make a script both importable and executable" 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 这句话,可能一开始听的还不是很懂.下面举例说明: 先写一个模块: ? 1 2 3 4 5 #module.py def main():   print "we are in %s"%__name__ if __name__ == '__main__':   ma

python中__name__ = '__main__' 的作用

有句话经典的概括了这段代码的意义: "Make a script both importable and executable" 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 先写一个模块: #module.py def main(): print "we are in %s"%__name__ if __name__ == '__main__': main() 这个函数定义了一个main函数,我们执行一下该py文件发现结果是打印出&

python中 __name__=='__main__' 的作用

很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = '__main__' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and executable" 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 这句话,可能一开始听的还不是很懂.下面

Python中 pass语句的作用

Python中的pass语句作用是什么?表示它不做任何事情,一般用做占位语句.pass语句具体作用及使用方法,我们往下看. pass语句在函数中的作用 当你在编写一个程序时,执行语句部分思路还没有完成,这时你可以用pass语句来占位,也可以当做是一个标记,是要过后来完成的代码.比如下面这样: >>>def iplaypython(): >>>       pass 定义一个函数iplaypython,但函数体部分暂时还没有完成,又不能空着不写内容,因此可以用pass来替

【转】浅析python 中__name__ = '__main__' 的作用

原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ = '__main__' 的作用,对于初学者来说很有帮助,需要的朋友可以参考下 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = '__main__

[转载]python中的@符号的作用

原文地址:python中的@符号的作用作者:queerfisher '@'符号用作函数修饰符是python2.4新增加的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.也就是说@A def f(): 是非法的. 只可以在模块或类定义层内对函数进行修饰,不允许修修饰一个类.一个修饰符就是一个函数,它将被修饰的函数做为参数,并返回修饰后的同名函数或其它可调用的东西. 实例(1): def spamrun(fn):     def sayspam(*args):         pri