[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 ...
随机推荐
- Android5.0以上系统的移动网络开关
笔者近期遇到一个非常有意思的bug,贴出来和大家分享下. 那是一个温暖的早晨,阳光晒得人非常舒服.一封bug邮件像一片叶子飘到我的邮箱. 一番交流.笔者确认负责的Widget开关在Android5.0 ...
- 11.怎样自学Struts2之Struts2验证[视频]
11.怎样自学Struts2之Struts2验证[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了.仅仅好传到百度云上: http://pan. ...
- Appium - Android 对照 iOS
Appium - Android 对照 iOS 作者: Max.Bai 时间: 2014/10 Appium - Android 对照 iOS Appium 支持Android也支持iOS.可是两者还 ...
- MyEclipse常见错误汇总,中英注释版(长期更新)
No.1 当一条语句漏写分号时错误描述如下 Syntax error, insert ";" to complete Statement(语法错误:插入分号完成语句描述) No.2 ...
- 提高FPGA速度的quartus编译选项
Turning on some optimizations in Quartus II may help increase it. Here are some you may want to try: ...
- bzoj1497: [NOI2006]最大获利(最大权闭合子图)
1497: [NOI2006]最大获利 题目:传送门 题解: %%%关于最大权闭合子图很好的入门题 简单说一下什么叫最大权闭合子图吧...最简单的解释就是正权边连源点,负权边连汇点(注意把边权改为正数 ...
- 【基础篇】Android中获取Drawable的方法
public static Drawable getDrawable(Context context,String filename) { BitmapDrawable drawable=null; ...
- UVa 208 Firetruck【回溯】
题意:给出一个n个节点的无向图,以及某个节点k,按照字典序从小到大输出从节点1到节点k的所有路径 看的题解 http://blog.csdn.net/hcbbt/article/details/975 ...
- appium使用教程(三)-------------用例编写
1. 驱动 import os, time, unittest from appium import webdriver PATH = lambda p:os.path.abspath(os.path ...
- 微信小程序 获取数组长度
wxml中直接 {{array.length}} js中 array.length 小程序调用API返回的数据全部都是异步的:所以前提是要确保array中的数据,是存在的