在定义局部变量前在函数中使用局部变量(此时有与局部变量同名的全局变量存在)
在函数中使用局部变来那个而同时又存在同名全局变量时是很复杂的,
使用规则:如果在函数中定义了任何东西,如果它只是在函数中使用那它就是局部的,反之就是全局变量。
意味着你不能在定义它之前把它当全局变量在函数中使用。

UnboundLocalError: local variable 'foo' referenced before assignment Python常见错误的更多相关文章

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

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

  2. _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 ...

  3. UnboundLocalError: local variable 'range' referenced before assignment

    1. 报错信息 UnboundLocalError: local variable 'range' referenced before assignment 2. 代码 class Car(): &q ...

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

  5. 变量引用的错误:UnboundLocalError: local variable 'range' referenced before assignment

    class Battery(): """一次模拟电瓶汽车的简单尝试""" def __init__(self,battery_size = ...

  6. 出现UnboundLocalError: local variable 'a' referenced before assignment异常的情况与解决方法

    出现UnboundLocalError: local variable ‘a’ referenced before assignment异常的情况与解决方法字面意思:局部变量赋值前被引用原因:局部变量 ...

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

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

  8. 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment

    总结: 内部函数,不修改全局变量可以访问全局变量 内部函数,修改同名全局变量,则python会认为它是一个局部变量 在内部函数修改同名全局变量之前调用变量名称(如print sum),则引发Unbou ...

  9. UnboundLocalError: local variable ‘xxx‘ referenced before assignment

    原因 在Python函数中调用了某个和全局变量同名的局部变量,导致编译器不知道此时使用的是全局变量还是局部变量 a = 3 def func(): a+=3 func() UnboundLocalEr ...

随机推荐

  1. js 实现前端路由的方法

    js 实现前端路由的方法 前端路由原理 History API https://developer.mozilla.org/en-US/docs/Web/API/History_API https:/ ...

  2. javascript & global event & custom event

    javascript & global event & custom event new CustomEvent object let event = new CustomEvent( ...

  3. Regular Expressions all in one

    Regular Expressions all in one Regular Expressions Cheatsheet https://developer.mozilla.org/en-US/do ...

  4. Typescript All In One

    Typescript All In One TypeScript 3.5 is now available. https://www.typescriptlang.org/#download-link ...

  5. CSS3 & gradient & color & background

    CSS3 & gradient & color & background css background https://developer.mozilla.org/en-US/ ...

  6. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

  7. TypeScript & Examples

    TypeScript & Examples http://www.typescriptlang.org/samples/index.html https://github.com/Micros ...

  8. arrayBuffer 转base64

    var buffer = new ArrayBuffer(8);// buffer 是接收到后台的流 function _arrayBufferToBase64( buffer ) { var bin ...

  9. 怎么创建CSV文件和怎么打开CSV文件

    CSV(Comma Separated Values逗号分隔值). .csv是一种文件格式(如.txt..doc等),也可理解.csv文件就是一种特殊格式的纯文本文件.即是一组字符序列,字符之间已英文 ...

  10. 宝塔面板配置Let's Encrypt证书自动续签失效及解决方案

    一.背景小故事 笔者手里有个朋友交给我去日常运维项目是PHP+微信小程序,部署在Linux系统上. 这个项目是用宝塔面板去进行日常的可视化运维管理,用起来蛮香的. 如不清楚宝塔的同学,可以自行了解,这 ...