Following code explain how 'global' works in the distinction of global variable and local variable.

 var = 'Global Variable'
print(var) def func1():
var = 'Local Variable'
print(var) def func2():
print(var) def func3():
global var
print (var)
var = 'Global Variable Changed in Function' func1()
func2()
func3() print(var)

Expected Result:

Global Variable

Local Variable

Global Variable

Global Variable

Global Variable Changed in Function

Global & Local Variable in Python的更多相关文章

  1. python的UnboundLocalError: local variable 'xxx' referenced b

    一.意思: 本地变量xxx引用前没定义. 二.错误原因     在于python没有变量的声明 , 所以它通过一个简单的规则找出变量的范围 :如果有一个函数内部的变量赋值 ,该变量被认为是本地的,所以 ...

  2. Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment

    参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...

  3. 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment

    闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...

  4. python 错误--UnboundLocalError: local variable '**' referenced before assignment

    val = 9 def test(flag): if flag: val = 1 else: print("test") return val if __name__ == '__ ...

  5. [合集]解决Python报错:local variable 'xxx' referenced before assignment

    a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...

  6. python: local variable 'xxx' referenced before assignment

    问题发现 xxx = 23 def PrintFileName(strFileName): if xxx == 23: print strFileName xxx = 24 PrintFileName ...

  7. 解决Python报错:local variable 'xxx' referenced before assignment(引)

    解决Python报错:local variable 'xxx' referenced before assignment(引) 解决Python报错:local variable 'xxx' refe ...

  8. _markupbase.py if not match: UnboundLocalError: local variable 'match' referenced before assignment,分析Python 库 html.parser 中存在的一个解析BUG

    BUG触发时的完整报错内容(本地无关路径用已经用 **** 隐去): **************\lib\site-packages\bs4\builder\_htmlparser.py:78: U ...

  9. Can we access global variable if there is a local variable with same name?

    In C, we cannot access a global variable if we have a local variable with same name, but it is possi ...

随机推荐

  1. KMP算法(模板)

    话说kmp真的挺难理解的,花了挺大功夫的,恩,找了段好理解的代码,做模板了 int KMP(char *s,char *p){ int ans = -1; nex[0] = 0; int lenp = ...

  2. iOS9上的Universal Link实现(教程)

    1.Universal Link 理解为苹果官方支持deeplink就行了 2.通过点击HTTP链接启动APP Web・iOS应用在支持Universal Link的前提下,当用户点击特点的链接时会自 ...

  3. 了解node.js

    转载自http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb 当我向人们介绍nod ...

  4. WPF button 圆角制作

    将以下节点复制到app.xaml的<Application.Resources>节点下 <Style TargetType="{x:Type Button}"&g ...

  5. Flex开发小结(1)如何使用AdvancedDataGrid

    1.AdvancedDataGrid扩展了普通DataGrid的功能,AdvancedDataGrid控件提供了另外一些特性,并在数据显示.数据聚合和数据格式化方面有着强大的控制力. 这里我主要说一下 ...

  6. one problem about Apple Keychain in use

    解决方案 Add Security.framework, then rebuild. Sometimes I find I have to build clean and then rebuild. ...

  7. JDBC学生管理系统--处理分页显示

    分页的思想: 假设一共有104条数据,每页显示10条数据: select * from student limit 0,10; 页数是index,第index页,对应的sql语句是: select * ...

  8. VC/MFC 下 递归遍历目录下的所有子目录及文件

    在MFC下要实现文件夹的递归遍历,可用CFileFind类,依次读取文件夹下的子文件夹和文件,并判断通过判断是文件夹还是文件来决定递归遍历.递归遍历代码如下: /******************* ...

  9. ListView simpleAdapter的基本使用

    使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行.HashMap的每个键 值数据映射到布局文件中对应id的组件上.因为系统没有对 ...

  10. Windows7服务无法启动的解决

    这周六,我接到了一个很诡异的案例,表现为任务栏右下角网络连接图标始终为一个红叉,已排除网卡硬件.链路和网卡驱动的问题.主板都新换了一块,可是问题依旧,这无疑将问题的根源指向了操作系统.本想通过网络疑难 ...