global和nonlocal的用法
1 nonlocal声明的变量不是局部变量,也不是全局变量,而是外部嵌套函数内的变量.写在内部嵌套函数里面,它实质上是将该变量定义成了全局变量,它等价于用两个global来定义该变量.只不过用两个global来实现太繁琐.只用一个global的话无法在这儿(嵌套函数中)实现.
def make_counter():
global count
count = 0
def counter():
# nonlocal count
global count
count += 1
return count
return counter
mc = make_counter()
print(mc())
print(mc())
print(mc())
#
#
#
2 利用global可将函数的局部变量变为全局变量
# 全局变量在函数内部可以任意调用
hehe=6
def f():
print(hehe)
f()
print(hehe)
#
# # 注意这里在函数先输出hehe变量时,即先使用了它,后定义它是2,所以程序认为它是局部变量,会报先使用后定义的错误
hehe=6
def f():
print(hehe)
hehe=2
f()
print(hehe)
# UnboundLocalError: local variable 'hehe' referenced before assignment # 这个是先定义后使用,函数内的hehe同样是局部变量
hehe=6
def f():
hehe=2
print(hehe)
f()
print(hehe)
#
# hehe=6
def f():
global hehe
print(hehe)
hehe=3
f()
print(hehe)
#
#
参考: https://www.cnblogs.com/summer-cool/p/3884595.html
https://www.cnblogs.com/yuzhanhong/p/9183161.html
https://www.liaoxuefeng.com/wiki/1016959663602400/1017434209254976#0
https://www.cnblogs.com/tallme/p/11300822.html
global和nonlocal的用法的更多相关文章
- Python Global和Nonlocal的用法
nonlocal 和 global 也很容易混淆.简单记录下自己的理解. 解释 global 总之一句话,作用域是全局的,就是会修改这个变量对应地址的值. global 语句是一个声明,它适用于整个当 ...
- python中global和nonlocal用法的详细说明
一.global 1.global关键字用来在函数或其他局部作用域中使用全局变量.但是如果不修改全局变量也可以不使用global关键字. gcount = 0 def global_test(): ...
- Python_动态参数、名称空间、作用域、作用域链、加载顺序、函数的嵌套、global、nonlocal
1.动态参数 当实参数量与形参数量相等时,参数传递正常. def func1(a, b, c): pass func1(1, 2, 3) 当实参数量与形参数量不相等时,则会报错. def func1( ...
- Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global以及nolocal的用法
Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global以及nolocal的用法 目录 Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global ...
- python中global 和 nonlocal 的作用域
python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 . 一 global global关键字用来在函数或其他局部作用域 ...
- Python--day13(函数嵌套定义,global、nonlocal、闭包函数、装饰器)
今日主要内容 1. 函数的嵌套定义 2. global.nonlocal关键字 3. 闭包及闭包的应用场景 4. 装饰器 1. 函数的嵌套定义 概念:在一个函数的内部定义另一个函数 为什么要有 ...
- day13(函数嵌套定义,global,nonlocal关键字,闭包,装饰器)
一,复习 ''' 1.函数对象:函数名 => 存放的是函数的内存地址 1)函数名 - 找到的是函数的内存地址 2)函数名() - 调用函数 => 函数的返回值 eg:fn()() => ...
- python第十三天,函数的嵌套定义,global,nonlocal关键字的使用,闭包及闭包的运算场景,装饰器
今日内容 1. 函数的嵌套定义 2.global,nonlocal关键字 3.闭包及闭包的运用场景 4.装饰器 函数的嵌套定义 1. 概念:在一个函数内部定义另一个函数 2 .为什么要有函数的嵌套定义 ...
- python13 1.函数的嵌套定义 2.global、nonlocal关键字 3.闭包及闭包的运用场景 4.装饰器
## 复习 '''1.函数对象:函数名 => 存放的是函数的内存地址1)函数名 - 找到的是函数的内存地址2)函数名() - 调用函数 => 函数的返回值 eg:fn()() =&g ...
随机推荐
- Centos7 初始化
systemctl disable firewalld sed -ri '/^[^#]*SELINUX=/s#=.+$#=disabled#' /etc/selinux/config grubby - ...
- linux pip使用国内源
最近在Linux里面使用pip安装应用的速度十分的慢,于是便上网找了一些国内的源. 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https:// ...
- Zabbix Agent配置文件详解
# This is a config file for the Zabbix agent daemon (Unix)# To get more information about Zabbix, vi ...
- JS 深拷贝/合并
var mix = function(r, s, ov) { if (!s || !r) return r; if (ov === undefined) ov = true; for (var p i ...
- iOS中为控件设置颜色渐变和透明度渐变
项目中用到地图设置渐变色,查找资料找到两种方法:一种设置颜色,一种设置透明度: //为颜色设置渐变效果: UIView *view = [[UIView alloc] initWithFrame:CG ...
- django 做 migrate 时 表已存在的处理方法
django 做 migrate 时 表已存在的处理方法 文章来源:嗨学网 http://www.piaodoo.com 在开发web的时候,如果是以前已存在的项目,项目下载下来后,为了使用测试库的数 ...
- 【leetcode】1244. Design A Leaderboard
题目如下: Design a Leaderboard class, which has 3 functions: addScore(playerId, score): Update the leade ...
- @Data注解使用后get set报错解决方法
Maven项目中已经导入相关的lombok.jar包但是使用后仍提示无set/get方法 .在idea中安装如下插件,安装后重启idea可用不报错. 转载于:https://www.cnblogs.c ...
- BZOJ 4154: [Ipsc2015]Generating Synergy KDtree+dfs序
多组数据真tm恶心~ 把 $dfs$序和深度分别看作横纵坐标,然后用 $KDtree$ 数点就可以了~ #include <cstdio> #include <cstring> ...
- Lucas定理初探
1.1 问题引入 已知\(p\)是一质数,求\(\dbinom{n}{m}\pmod{p}\). 关于组合数,它和排列数都是组合数学中的重要概念.这里会张贴有关这两个数的部分内容. 由于Lucas定理 ...