问题发现

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")
File "C:/Users/jihite/Desktop/del.py", line 3, in PrintFileName
if xxx == 23:
UnboundLocalError: local variable 'xxx' referenced before assignment

意思说局部变量‘xxx’前边没有定义,但是最前面不是定义了吗。注意这里提示是局部变量,一开始定义的为全局变量。如果这里定义的就是全局变量可以通过关键字global来说明

xxx = 23
def PrintFileName(strFileName):
if xxx == 23:
global xxx
print strFileName
xxx = 24 PrintFileName("file")

运行正常。

但是这样也是没错

xxx = 23
def PrintFileName(strFileName):
if xxx == 23:
print xxx PrintFileName("file")

问题所在

在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关键字。

python: local variable 'xxx' referenced before assignment的更多相关文章

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

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

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

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

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

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

  4. Python UnboundLocalError: local variable 'xxx' referenced before assignment 解决方法

    一.报错含义: val=9 def test(): print(val) val = 6 print(val) test() 翻译:本地变量xxx引用前没有定义. 二.报错原因 这是Python变量作 ...

  5. python:UnboundLocalError: local variable 'xxx' referenced before assignment

    近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...

  6. local variable 'xxx' referenced before assignment

    这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...

  7. local variable 'xxx' referenced before assignment(犯过同样的错)

    这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...

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

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

  9. python 使用嵌套函数报local variable xxx referenced before assignment或者 local variable XXX defined in enclosing scope

    情况一: a 直接引用外部的,正常运行 def toplevel(): a = 5 def nested(): print(a + 2) # theres no local variable a so ...

随机推荐

  1. centos7 安装dnf包管理器和常用命令

    Installing DNF Currently the DNF package comes from the EPEL repository, so if your Linux system is ...

  2. 操作Word的辅助类(word2003)

    该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插入图片 (3)插入表格 (4)载入模版 (5)编辑模版,利用标签等 (6)插入页眉页脚 /*************** ...

  3. silverlight PopupWindow Resizeable兼容问题

    下方第一段代码,在ie11中Resizeable无法生效,而在chrome中运行正常. HtmlPopupWindowOptions options = new HtmlPopupWindowOpti ...

  4. on where having总结

    1. ON 和WHERE 所有的查询都回产生一个中间临时报表,查询结果就是从返回临时报表中得到.ON和WHERE后面所跟限制条件的区别,主要与限制条件起作用的时机有关, ON根据限制条件对数据库记录进 ...

  5. vector妙用轻松水过平衡树???

    极短代码预警 今天听身边的神仙说,可以用vector来写平衡树,代码极短. 然后去网上搜了一下,看到了attack dalao的这篇文章. 蒟蒻表示ssfd 赶紧膜拜了一波,并发表了一篇博客表示纪念. ...

  6. 932. Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  7. OCP 12c最新考试原题及答案(071-4)

    4.(4-11) choose two:View the Exhibit and examine the data in the PRODUCT_INFORMATION table.Which two ...

  8. 二 ,Smarty模板技术/引擎——变量操作(1)

    1,基本变量 $smarty->assign('data1',3); $smarty->assign('data2',3.45); $smarty->assign('data3',' ...

  9. java字段中初始化的规律与如何用静态成员函数调用非静态成员

    java字段中初始化的规律: 执行以下代码,出现的结果是什么? class InitializeBlockClass{ { field=200; } public int field=100; pub ...

  10. leetcode-812-Largest Triangle Area

    题目描述: You have a list of points in the plane. Return the area of the largest triangle that can be fo ...