python中nonlocal 的作用域
'''
nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量。
'''
def work():
x = 0
def new_work():
nonlocal x
x=x+3
return x
return new_work f=work()
print(f())
print(f())
print(f())
打印结果
3
6
9
'''
使用global 实现
'''
a =0
def new_work():
global a
a=a+3
return a
print(new_work())
print(new_work())
print(new_work())
打印结果
3
6
9
'''
闭包= 函数+环境变量
''' def dosometing():
a =25
def add(x):
d=a+x
return d
return add
a = 10
f=dosometing()
print(f(5))
print(f(5))
print(f(5))
打印结果
30
30
30
'''
闭包= 函数+环境变量
nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量。
''' def dosometing():
a =25
def add(x):
nonlocal a
a=a+x
return a
return add
a = 10
f=dosometing()
print(f(5))
print(f(5))
print(f(5))
打印结果
30
35
40
python中nonlocal 的作用域的更多相关文章
- Python中命名空间与作用域使用总结
1 引言 命名空间与作用域是程序设计中的基础概念,深入理解有助于理解变量的生命周期,减少代码中的莫名其妙bug.Python的命名空间与作用域与Java.C++等语言有很大差异,若不注意,就可能出现莫 ...
- Python中变量的作用域(variable scope)
http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 #!/usr/bin/ ...
- Python中的变量作用域
python中变量作用域包括: L (Local) 局部作用域,函数内部声明但没有使用global的变量E (Enclosing) 闭包函数外的函数中,def或者lambda的本地作用域G (Glob ...
- python学习之【第九篇】:Python中的变量作用域
1.前言 Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的. 2.变量作用域 变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python的作 ...
- Python中变量的作用域
一.变量作用域的含义 变量的作用域说白了就是变量的值从哪里获取,或者说变量取值的地方 我们在写代码过程中会用到很多变量,这些变量会出现在各种代码块中,有的出现在函数块里,有的在函数块外,例如: def ...
- python中global的作用域
#python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 . ''' a=30 声明为全局变量 a=20 为test()函 ...
- Python中nonlocal的用法
class Text: def __init__(self): pass def big(self): n, m = 0, 0 def a(): nonlocal n n += 1 print(n) ...
- 论python中的作用域
编程语言从早至今,可以分为面向过程编程.面向函数编程和面向对象编程.BASIC语言是典型的面向过程编程的语言,C语言支持面向函数编程,但不支持面向对象,JAVA只支持面向对象编程,python同时支持 ...
- Python进阶 - 命名空间与作用域
Python进阶 - 命名空间与作用域 写在前面 如非特别说明,下文均基于Python3 命名空间与作用于跟名字的绑定相关性很大,可以结合另一篇介绍Python名字.对象及其绑定的文章. 1. 命名空 ...
随机推荐
- 编程语言-Java-问题整理
jar文件运行报错 - Exception in thread "main" java.lang.UnsupportedClassVersionError 低版本运行高版本文件 ...
- Node.js实战6:定时器,使用timer延迟执行。
setTimeout 在nodejs中,通过setTimeout函数可以达到延迟执行的效果,这个函数也常被称为定时器. 一个简单的例子: console.log( (new Date()).getSe ...
- JavaScript defineProperties
function defineProperties(obj, properties) { function convertToDescriptor(desc) { function h ...
- TypeError: 'generator' object is not subscriptable
TypeError: 'generator' object is not subscriptable 生成器对象不可以带下标 def get_row(self,row_no): if not isin ...
- 三级级联(js实现)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- aspnet core in docker
1 创建一个文件夹(app), 将项目发布后的文件放入该文件夹中 并且创建Dockerfile文件 2 打开Dockerfile文件,编辑一下内容 #基于 `microsoft/dotnet:-cor ...
- 创建配置中心服务端(Spring Cloud Config)
创建配置中心服务端 创建好项目后添加配置文件内容 server.port=9004 spring.application.name=spring-cloud-config-server-01 #git ...
- bfs(火星撞地球)
Meteor Shower 链接:https://ac.nowcoder.com/acm/contest/997/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...
- 阿里云云监控agent插件 - Linux版
阿里云云监控agent插件使用指南 1.安装(注意,要以“root”权限运行,复制 sudo后面的就行,别把#也复制进去) #64位 # sudo bash -c "wget -e 'htt ...
- 77.LRU Cache(最近最久未使用算法)
Level: Hard 题目描述: Design and implement a data structure for Least Recently Used (LRU) cache. It sh ...