Python状态保持

​ 全局 global

 def tester(start):
global state
state = start
def nested(label):
global state
print(label,state)
state += 1
return nested # 都声明为全局,只会保存一个副本,会覆盖
    

​ 非本地 nonlocal

 def tester(start):
state = start
def nested(label):
nonlocal state
print(label,state)
state += 1
return nested # Python3 主流 但是作用域只能是嵌套作用域
       

​ 类 class

 class nested():
def __init__(self,state):
self.state = state
def __call__(self,label):
print(self.state,label)
self.state += 1 # 有点老了

函数属性 函数名.变量名

 def tester(start):
def nested(label):
print(label,nested.state)
nested.state += 1
nested.state = start
return nested # 未曾用过的黑魔法,这回就知道了

最新文章

  1. [LeetCode] Game of Life 生命游戏
  2. C# 中的委托和事件(转载)
  3. js 扩展Array支持remove方法
  4. bash操作小结
  5. 更加优雅地配置Spring Securiy(使用Java配置和注解)
  6. MyEclipse自动补全与快捷键设置
  7. 【缓存】Sql Server 2005/2008 SqlCacheDependency查询通知的使用总结
  8. linux语言环境设置
  9. jQuery查看dom元素上绑定的事件列表
  10. android在广播接收器BroadcastReceiver里面再进行发送广播,造成当前广播接收器不断循环执行问题
  11. Docker image 镜像介绍
  12. Android应用程序与SurfaceFlinger服务的关系概述和学习计划
  13. Linkedin工程师是如何优化他们的Java代码的(转)
  14. Python [目录]
  15. 使用kermit通过串口升级uboot
  16. day 21 内存管理,正则
  17. Linux 入门视频教程
  18. Spring Security Oauth2 示例
  19. C#闰年判断
  20. C++ main函数的参数

热门文章

  1. 死磕 java同步系列之开篇
  2. Protocol Buffers官方文档(开发指南)
  3. CEPH安装教程(上)
  4. [Windows]获取当前时间(年/月/日/时/分/秒)
  5. java 提取(解压)zip文件中特定后缀的文件并保存到指定目录
  6. DMZ的原理与应用
  7. Topoi 测验1301, 问题C: 1959: 解题 解题报告
  8. array类型的方法
  9. Storm概念学习系列 之Worker工作者进程
  10. 【Linux】Xshell-Linux常用命令