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的更多相关文章

  1. 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 ...

  2. python学习笔记(python简史)

    一.python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum) 目前python主要应用领域: ·云计算 ·WEB开发 ·科学运算.人工智能 ·系统运维 ·金融:量化交 ...

  3. Python 基础之四初识Python数据类型

    数字 Int,整型 Float,浮点型 Long,长整型 布尔 字符串 列表 元组 字典 1.数字 INT(整型) 在32位系统上,整数的位数为32位,取值范围为-2**31~2**31-1,即-21 ...

  4. 我的Python学习之路 Python的输入输出与基本数据类型

    *** python中的变量不需要事先声明再使用,而可以直接来一个变量名,后面一个赋值,接着一个数据值,如 hw = "hello python",相当于Python能智能的根据你 ...

  5. 我的Python学习之路 Python的初识与准备工作

    注:文笔不好,不喜勿喷,当个段子看看就好 一.初识Python 第一次听到Python是在2016年大概暑假 时候(即将大三),因为对黑客技术的蜜汁热爱(虽然自己并不会),在玄魂大大的公众微信号中看到 ...

  6. 进击的Python【第一章】:Python背景初探与Python基础(一)

    Python背景初探 一.Python起源 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做 ...

  7. Python之路,Day1 - Python基础1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  8. Python::re 模块 -- 在Python中使用正则表达式

    前言 这篇文章,并不是对正则表达式的介绍,而是对Python中如何结合re模块使用正则表达式的介绍.文章的侧重点是如何使用re模块在Python语言中使用正则表达式,对于Python表达式的语法和详细 ...

  9. python 2.4 与 python 3.0 的比较

    转过来,留着日后查看 [转自:]http://hi.baidu.com/autoitcn/blog/item/5f41973294b5fc4fac4b5f77.html python 2.4 与 py ...

随机推荐

  1. 直接插入排序(Straight Insertion Sort)

    直接插入排序(Straight Insertion Sort)的基本操作是将一个记录插入到已经排好序的有序表中,从而得到一个新的.记录数增1的有序表. /* 对顺序表L作直接插入排序 */ void ...

  2. 日期格式,Popup的使用方法,RenderTransform与LayoutTransform的区别

    1.画个笑脸给大家娱乐一下: <Canvas Width="200" Height="180" VerticalAlignment="Cente ...

  3. 构造函数的理解(构造函数与 init 方法)

    0. 构造函数与 init 方法 构造方法内部禁止添加任何业务逻辑,如果有业务逻辑,请放在 init 方法中: 1. 构造函数的参数 以下为一个堆实现优先队列(堆的实现借助完全二叉树,而完全二叉树又可 ...

  4. django 笔记16 文件上传笔记

    views.py文件 def upload_file(request): username = request.POST.get('username') fafafa = request.FILES. ...

  5. Objects are mutable

    We can change the state of an object by making an assignment to one of its attributes. For example, ...

  6. android客户端向java服务端post发送json

    android 端: private void HttpPostData() {        try { HttpClient httpclient = new DefaultHttpClient( ...

  7. Java中MySQL事务处理举例

    实例(以sql语句中的insert语句为例) import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...

  8. 【AngularJS学习笔记】AngularJS表单验证

    AngularJS表单验证 AngularJS提供了一些自带的验证属性 1.novalidate:添加到HTML的表单属性中,用于禁用浏览器默认的验证. 2.$dirty   表单有填写记录 3.$v ...

  9. <Sicily>Catch the thief

    一.题目描述 A thief has robbed a bank in city 1 and he wants to go to city N. The police know that the th ...

  10. ES6中的let、contst

    一 let 1.let 局部变量 不会变量提升,在运用时候要先声明在调用,var 全局变量 会产生变量提升: 2.在块级作用域中纯在let const,他所生命的变量就绑定在这个区域,未经过声明调用会 ...