python - Local variable referenced before assignment
来源:互联网 发布:linux 进程调度 编辑:程序博客网 时间:2024/05/10 10:23
直接上代码
def foo():a = 1def bar():a = a + 1return bar()
运行foo(),报错
import disdef foo():a = 1def bar():a = a + 1dis.dis(bar)return bar()
dis打印的内容如下:
很容易明白其意思,,应该是从某个地方加载a,1,执行加法,写回a;
在CPython源码中,搜索“referenced before assignment”
#define NAME_ERROR_MSG \ "name '%.200s' is not defined"#define GLOBAL_NAME_ERROR_MSG \ "global name '%.200s' is not defined"#define UNBOUNDLOCAL_ERROR_MSG \ "local variable '%.200s' referenced before assignment"#define UNBOUNDFREE_ERROR_MSG \ "free variable '%.200s' referenced before assignment" \ " in enclosing scope"
接着搜索UNBOUNDLOCAL_ERROR_MSG
TARGET(LOAD_FAST) x = GETLOCAL(oparg); if (x != NULL) { Py_INCREF(x); PUSH(x); FAST_DISPATCH(); } format_exc_check_arg(PyExc_UnboundLocalError, UNBOUNDLOCAL_ERROR_MSG, PyTuple_GetItem(co->co_varnames, oparg)); break;
TARGET(DELETE_FAST) x = GETLOCAL(oparg); if (x != NULL) { SETLOCAL(oparg, NULL); DISPATCH(); } format_exc_check_arg( PyExc_UnboundLocalError, UNBOUNDLOCAL_ERROR_MSG, PyTuple_GetItem(co->co_varnames, oparg) ); break;
static voidformat_exc_unbound(PyCodeObject *co, int oparg){ PyObject *name; /* Don't stomp existing exception */ if (PyErr_Occurred()) return; if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { name = PyTuple_GET_ITEM(co->co_cellvars, oparg); format_exc_check_arg( PyExc_UnboundLocalError, UNBOUNDLOCAL_ERROR_MSG, name); } else { name = PyTuple_GET_ITEM(co->co_freevars, oparg - PyTuple_GET_SIZE(co->co_cellvars)); format_exc_check_arg(PyExc_NameError, UNBOUNDFREE_ERROR_MSG, name); }}
初步判断是在执行LOAD_FAST时,报出的错误;
如果能够调试就好了,以后试试吧;
顺便提一下,CpythonDoc目录下有这样的解释
>>> x = 10
>>> def foo():
... print(x)
... x += 1
results in an UnboundLocalError:
>>> foo()
Traceback (most recent call last):
...
UnboundLocalError: local variable 'x' referenced before assignment
This is because when you make an assignment to a variable in a scope, that
variable becomes local to that scope and shadows any similarly named variable
in the outer scope. Since the last statement in foo assigns a new value to
``x``, the compiler recognizes it as a local variable. Consequently when the
earlier ``print(x)`` attempts to print the uninitialized local variable and
an error results.
- Python - UnboundLocalError: local variable referenced before assignment
- python - Local variable referenced before assignment
- 【Python】UnboundLocalError: local variable 'l' referenced before assignment
- 【Python基础】之全局变量(UnboundLocalError: local variable referenced before assignment)
- 【Python】UnboundLocalError: local variable 'l' referenced before assignment
- 【python】UnboundLocalError: local variable 'counter' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- python:UnboundLocalError: local variable 'l' referenced before assignment
- 【Python】UnboundLocalError: local variable 'x' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- Python的UnboundLocalError: local variable 'xxx' referenced before assignment
- 解决Python报错:local variable 'xxx' referenced before assignment
- python 报错 :local variable 'totalCount' referenced before assignment
- python出错:local variable XXX referenced before assignment
- 【Python基础】Python3: UnboundLocalError: local variable 'num' referenced before assignment
- Python进阶(三十二)-local variable 'xxx' referenced before assignment
- Python的UnboundLocalError: local variable 'xxx' referenced before assignment
- 【Python异常】UnboundLocalError: local variable 'xxxx' referenced before assignment
- net jquery.pagination.js 无刷新分页 不需<%=count%>
- JAVASE自学笔记IO File
- ipconfig命令
- KMP模式匹配算法
- 软软解析之缓存游标的迷惑-----已解决!
- python - Local variable referenced before assignment
- C++_模板举例_使用模板实现集合类(堆栈)
- android R.java 文件丢失
- cocos2d-x-教程 让精灵响应触摸 并把方向旋转到相对应的角度
- C++_文件读写
- LDD3源码学习日记<七>
- route命令
- hdu-2161-Primes
- C++_文本文件读写常用代码