Global & Local Variable in Python

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

 1 var = ‘Global Variable‘
 2 print(var)
 3
 4 def func1():
 5     var = ‘Local Variable‘
 6     print(var)
 7
 8 def func2():
 9     print(var)
10
11 def func3():
12     global var
13     print (var)
14     var = ‘Global Variable Changed in Function‘
15
16 func1()
17 func2()
18 func3()
19
20 print(var)

Expected Result:

Global Variable

Local Variable

Global Variable

Global Variable

Global Variable Changed in Function

时间: 2024-10-09 03:08:35

Global & Local Variable in Python的相关文章

python: local variable 'xxx' referenced before assignment

问题发现 xxx = 23 def PrintFileName(strFileName): if xxx == 23: print strFileName xxx = 24 PrintFileName("file") 报错 Traceback (most recent call last): File "C:/Users/Desktop/del.py", line 7, in <module> PrintFileName("file"

python的UnboundLocalError: local variable &#39;xxx&#39; referenced before assignment

一.意思: 本地变量xxx引用前没定义. 二.错误原因 在于python没有变量的声明 , 所以它通过一个简单的规则找出变量的范围 :如果有一个函数内部的变量赋值 ,该变量被认为是本地的,所以如果有修改变量的值就会变成局部变量. 三.产生这个错误的场景 python代码:val=9def test(flag):      if flag:          val = 1      else:          print 'fuck'      return val test(0)错误提示:U

python 错误--UnboundLocalError: local variable &#39;**&#39; referenced before assignment

昨日想在python的一个函数中做一下发送次数的统计,需要用到全局变量,如下 1 COUNT = 0 2 3 def sendOneLineMsg(producer, listFromLine): 4 acSNStr = listFromLine[0] 5 macStr = listFromLine[1] 6 onlineTimeStr = listFromLine[2] 7 msg = {'clientMAC' : macStr, 'acSN' : acSNStr, 'onLineTime'

【python】UnboundLocalError: local variable &#39;counter&#39; referenced before assignment

http://passport.baidu.com/?business&un=%E4%BA%91%E9%BE%99%E5%8E%BF%5F%E5%B0%8F%E5%A7%90%5F%E6%89%BE#0 http://passport.baidu.com/?business&un=%E6%89%BE%5F%E6%B4%B1%E6%BA%90%E5%8E%BF%5F%E7%BE%8E%E5%A5%B3#0 http://passport.baidu.com/?business&un=

Python | local variable &#39;xxxx&#39; referenced before assignment

>>> def func(num): ... def func_in(): ... num += 1 ... print(num) ... return func_in ... >>> fun = func(10) >>> fun <function func.<locals>.func_in at 0x1034410d0> >>> fun() Traceback (most recent call last)

全局变量报错:UnboundLocalError: local variable &#39;l&#39; referenced before assignment

总结: 内部函数,不修改全局变量可以访问全局变量 内部函数,修改同名全局变量,则python会认为它是一个局部变量 在内部函数修改同名全局变量之前调用变量名称(如print sum),则引发Unbound-LocalError 在程序中设置的sum属于全局变量,而在函数中没有sum的定义,根据python访问局部变量和全局变量的规则:当搜索一个变量的时候,python先从局部作用域开始搜索,如果在局部作用域没有找到那个变量,那样python就在全局变量中找这个变量,如果找不到抛出异常(NAMEE

遇到local variable &#39;e&#39; referenced before assignment这样的问题应该如何解决

问题:程序报错:local variable 'e' referenced before assignment 解决:遇到这样的问题,说明你在声明变量e之前就已经对其进行了调用,定位到错误的地方,对变量进行重新的声明 通常这样的问题对于python的程序员来说都是因为习惯了python2的语法,转移到python3中时,出现的错误.在Python3中,异常对象无法在异常块作用域外访问.(原因是在垃圾收集器运行且从内存中清理引用之前会在内存栈帧中保存一个引用周期)通常参考下面这个例子来做异常处理:

【翻译自mos文章】关于分区索引:Global, Local, Prefixed and Non-Prefixed

来源于: Partitioned Indexes: Global, Local, Prefixed and Non-Prefixed (文档 ID 69374.1) APPLIES TO: Oracle Database - Enterprise Edition - Version 8.0.3.0 and later Information in this document applies to any platform. PURPOSE To differentiate between typ

jsp的&lt;%@ include file=&quot;jsp/common.jsp&quot; %&gt;报错误Duplicate local variable basePath

将公共引入的文件放到common.jsp中,其他页面引入该jsp即可使用 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 +