This inspection warns about local variables referenced before assignment.
关于 local variable 'has' referenced before assignment 问题
今天在django开发时,访问页面总是出现错误提示“
local variable 'has' referenced before assignment
”,查了一下资料,好像是说无法访问这个变量,检查一下代码我的视图是这样写的:
def MusicTable(request):
MUSICIANS = [
{'name': 'Django Reinhardt', 'genre': 'jazz'},
{'name': 'Jimi Hendrix', 'genre': 'rock'},
{'name': 'Louis Armstrong', 'genre': 'jazz'},
{'name': 'Pete Townsend', 'genre': 'rock'},
{'name': 'Yanni', 'genre': 'new age'},
{'name': 'Ella Fitzgerald', 'genre': 'jazz'},
{'name': 'Wesley Willis', 'genre': 'casio'},
{'name': 'John Lennon', 'genre': 'rock'},
{'name': 'Bono', 'genre': 'rock'},
{'name': 'Garth Brooks', 'genre': 'country'},
{'name': 'Duke Ellington', 'genre': 'jazz'},
{'name': 'William Shatner', 'genre': 'spoken word'},
{'name': 'Madonna', 'genre': 'pop'},]
Mu=[]
#预处理 判断是否粗体显示 ,模板只是呈现方式,不应该处理 判断哪些是特殊显示
for m in MUSICIANS:
if '' not in m['name']:
has = True
Mu.append({'name':m['name'],
'genre':m['genre'],
'is_important':m['genre'] in ('jazz','rock'),
'is_pretentious': ' ' not in m['name'],}
)
return render_to_response('Musictable.html', {'Mu': Mu,'has_pretentious':has,})
猛地一看变量has应该是有赋值啊,我郁闷了。
后来看到网上一个帖子说的也是这个问题
-------------------------------------------------------------------------------
程序大致是这样的:
CONSTANT = 0
def modifyConstant() :
print CONSTANT
CONSTANT += 1
return
if __name__ == '__main__' :
modifyConstant()
print CONSTANT
运行结果如下:
UnboundLocalError: local variable 'CONSTANT' referenced before assignment
看来,全局变量在函数modifyConstant中边成了局部变量,似乎全局变量没有生效?
做点修改:
CONSTANT = 0
def modifyConstant() :
print CONSTANT
#CONSTANT += 1
return
if __name__ == '__main__' :
modifyConstant()
print CONSTANT
运行正常,看来函数内部是可以访问全局变量的。
所以,问题就在于,因为在函数内部修改了变量CONSTANT,Python认为CONSTANT是局部变量,而print CONSTANT又在CONSTANT += 1之前,所以当然会发生这种错误。
那么,应该如何在函数内部访问并修改全局变量呢?应该使用关键字global来修饰变量(有点像PHP):
CONSTANT = 0
def modifyConstant() :
global CONSTANT
print CONSTANT
CONSTANT += 1
return
if __name__ == '__main__' :
modifyConstant()
print CONSTANT
就这么简单!
------------------------------------------------------------------------------------
看了上边帖子内容,我有了一点启发,仔细看一下我程序这里:
for m in MUSICIANS:
if '' not in m['name']:
has = True
Mu.append({'name':m['name'],
'genre':m['genre'],
'is_important':m['genre'] in ('jazz','rock'),
'is_pretentious': ' ' not in m['name'],}
)
return render_to_response('Musictable.html', {'Mu': Mu,'has_pretentious':has,})
标红的部分 ''中间没有空格,而在这个循环中根本没有一次能满足if '' not in m['name']: 这个条件,所以在 return render_to_response('Musictable.html', {'Mu': Mu,'has_pretentious':has,}) 传递 has的时候,报错。
解决办法有两个 一个是将if '' not in m['name']: 的''加上空格变成‘ ’。
第二个办法在之前给has一个初始值 has=False。
---------------------
作者:jiangnanandi
来源:CSDN
原文:https://blog.csdn.net/jiangnanandi/article/details/3553243
版权声明:本文为博主原创文章,转载请附上博文链接!
This inspection warns about local variables referenced before assignment.的更多相关文章
- python 错误--UnboundLocalError: local variable '**' referenced before assignment
val = 9 def test(flag): if flag: val = 1 else: print("test") return val if __name__ == '__ ...
- local variables referenced from a Lambda expression must be final or effectively final------理解
前几天使用lamdba时,报了一个这个错,原因是在lamdba体中使用了一个变量,觉得很奇怪! 今天在读这本书的时候,又看到了这个解释,这里有了更深刻的理解,总结一下: 在jdk1.8之前在使用匿名内 ...
- Effective Java 45 Minimize the scope of local variables
Principle The most powerful technique for minimizing the scope of a local variable is to declare it ...
- local variable 'xxx' referenced before assignment
这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...
- Implicitly Typed Local Variables
Implicitly Typed Local Variables It happens time and time again: I’ll be at a game jam, mentoring st ...
- 遇到local variable 'e' referenced before assignment这样的问题应该如何解决
问题:程序报错:local variable 'e' referenced before assignment 解决:遇到这样的问题,说明你在声明变量e之前就已经对其进行了调用,定位到错误的地方,对变 ...
- RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment
Issue: When you install RDO stack on CentOS, you may encounter following error. Error: [root@localho ...
- Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...
- 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment
闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...
随机推荐
- API参考文档
Android 中文版:http://www.apiref.com/android-zh/index.html Bootstrap3 教程:http://www.apiref.com/bootstra ...
- PHP生成PDF完美支持中文,解决TCPDF乱码
PHP生成PDF完美支持中文,解决TCPDF乱码 2011-09-26 09:04 418人阅读 评论(0) 收藏 举报 phpfontsheaderttfxhtml文档 PHP生成PDF完美支持中文 ...
- 转:Linux设备树(Device Tree)机制
目录 1. 设备树(Device Tree)基本概念及作用 2. 设备树的组成和使用 2.1. DTS和DTSI 2.2. DTC 2.3. DTB 2.4. Bootloader 3. 设备树中d ...
- 59、salesforce实现数据的批量处理
批处理,往自己的邮箱发一封邮件,批处理采用异步的处理方式处理数据,最多可以处理5000万条数据 global with sharing class MerchandiseBatch implement ...
- VPS性能测试:CPU内存,硬盘IO读写,带宽速度,UnixBench和压力测试
现在便宜的VPS主机越来越多了,一些美国的VPS主机甚至给出1美元一月的VPS,堪比虚拟主机还要便宜,巨大的价格优势吸引不少人购买和使用,而近些年来国内的主机商也开始意识到便宜的VPS对草根站长的诱惑 ...
- 使用 vue.js 的一些操作记录
vue.js不支持ie8以下 1. 在html的属性中赋值: 需要在属性前加上 v-bind
- Supervisord rce(CVE-2017-11610)
POST /RPC2 HTTP/1.1 Host: localhost Accept: */* Accept-Language: en User-Agent: Mozilla/5.0 (compati ...
- Java web后台插入数据库中文乱码问题解决
前言:项目想要避免乱码情况的出现,要保持服务器.数据库.项目.以及前端编码一致.一.项目以及前端编码,设置. myeclipse--->右键项目--->Properties--->R ...
- CentOS 7.4安装telnet服务端
CentOS 7.4安装telnet服务端 安装xinetd服务 # yum -y install xinetd 安装telnet-server # yum -y install telnet-ser ...
- Python中xlrd、xlwt、win32com模块对xls文件的读写操作
# -*- coding: utf-8 -*- #xlrd和xlwt只支持xls文件读写,openpyxl只支持xlsx文件的读写操作 import xlrd import xlwt import w ...