[Python] Understand Scope in Python
Misunderstanding scope can cause problems in your application. Watch this lesson to learn how Python scope works and the hidden implications it presents. Local scope, nonlocal scope, and global scope variables are demonstrated and explained.
For example, we have the code:
def whoami():
def local_groot():
i = "I am local groot"
print(i) #"I am local groot" i = "I am groot"
print(i) # "I am groot" # Call the local groot function
local_groot()
print(i) # "I am groot"
Each function has its scope, won't conflict with outside function scope's variable.
For the case you do want to overwrite:
def whoami():
def local_groot():
i = "I am local groot"
print(i) def nonlocal_groot():
nonlocal i
i = "I am nonlocal groot" i = "I am groot"
print(i) #"I am groot"
nonlocal_groot()
print(i) #"I am nonlocal groot"
We can use 'nolocal' keyword.
There is also 'global' keyword, but you don't want to use it, it is not good pratice:
def global_groot():
global i
i = "I am global groot"
[Python] Understand Scope in Python的更多相关文章
- Introspection in Python How to spy on your Python objects Guide to Python introspection
Guide to Python introspection https://www.ibm.com/developerworks/library/l-pyint/ Guide to Python in ...
- python学习笔记(python简史)
一.python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum) 目前python主要应用领域: ·云计算 ·WEB开发 ·科学运算.人工智能 ·系统运维 ·金融:量化交 ...
- Python 基础之四初识Python数据类型
数字 Int,整型 Float,浮点型 Long,长整型 布尔 字符串 列表 元组 字典 1.数字 INT(整型) 在32位系统上,整数的位数为32位,取值范围为-2**31~2**31-1,即-21 ...
- 我的Python学习之路 Python的输入输出与基本数据类型
*** python中的变量不需要事先声明再使用,而可以直接来一个变量名,后面一个赋值,接着一个数据值,如 hw = "hello python",相当于Python能智能的根据你 ...
- 我的Python学习之路 Python的初识与准备工作
注:文笔不好,不喜勿喷,当个段子看看就好 一.初识Python 第一次听到Python是在2016年大概暑假 时候(即将大三),因为对黑客技术的蜜汁热爱(虽然自己并不会),在玄魂大大的公众微信号中看到 ...
- 进击的Python【第一章】:Python背景初探与Python基础(一)
Python背景初探 一.Python起源 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做 ...
- Python之路,Day1 - Python基础1
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Python::re 模块 -- 在Python中使用正则表达式
前言 这篇文章,并不是对正则表达式的介绍,而是对Python中如何结合re模块使用正则表达式的介绍.文章的侧重点是如何使用re模块在Python语言中使用正则表达式,对于Python表达式的语法和详细 ...
- python 2.4 与 python 3.0 的比较
转过来,留着日后查看 [转自:]http://hi.baidu.com/autoitcn/blog/item/5f41973294b5fc4fac4b5f77.html python 2.4 与 py ...
随机推荐
- sage开发url替换字符串
/// <summary> /// Url字段值替换,无该字段则加入 /// </summary> /// <param ...
- UI组件之AdapterView及其子类(四)Gallery画廊控件使用
听说 Gallery如今已经不使用了,API使用ViewPaper取代了,以后再学专研ViewPaper吧如今说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...
- bzoj1430: 小猴打架(prufer序列)
1430: 小猴打架 题目:传送门 简要题意: n只互不相识的猴子打架,打架之后就两两之间连边(表示已经相互认识),只有不认识(朋友的朋友都是朋友)的两只猴子才会打架.最后所有的猴子都会连成一棵树,也 ...
- 本地搭建MongoDB Server
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ Overview Requirements Get Mongo ...
- Codeforces 708D 费用流 (呃我们的考试题)
NB的题目背景 输入输出一样 考试的时候貌似只有gzz一个人搞出来了 %gzz 思路: 分情况讨论 add(x,y,C,E) C是费用 E是流量 1. f>c add(x,y,2,inf),ad ...
- PostgreSQL Replication之第三章 理解即时恢复(4)
3.4 重放事务日志 一旦我们创建了一个我们自己的初始基础备份,我们可以收集数据库创建的XLOG.当时间到时,我们可以使用所有这些XLOG 文件并执行我们所期望的恢复进程.这就像本节描述的一样工作. ...
- dedecms实现表单提交数据到指定的邮箱
1.http://blog.csdn.net/webnoties/article/details/17219219 2.http://www.jz96.com/451.html 3.https://m ...
- Decorator - 利用装饰器武装前端代码
历史 以前做后端时,接触过一点Spring,也是第一次了解DI.IOC等概念,面向切面编程,对于面向对象编程还不怎么熟练的情况下,整个人慌的一批,它的日志记录.数据库配置等都非常方便,不回侵入到业务代 ...
- bzoj 1588 [HNOI2002] 营业额统计 链表和Splay
来自HNOI 2002营业额的统计一题,这题以前是用链表水过的,最近看见许多splay的题,赶紧张一下知识. 题目大意就是对于一个序列,输出每个元素与它之前元素的差的最小值的和.先说链表的方法吧. 大 ...
- Android SDK Manager代理设置
1.SDK Manager中打开 Tools—>Manage Add-on Sites…—>User Defined Sites 2.New …输入: http://android-mir ...