python-3.x - 赋值前引用的局部变量“类型”
问题描述
为什么我会收到此错误?
赋值前引用的局部变量“类型”
代码:
try:
if type(meeting.modified_date) != bool:
//code
except Exception as e:
raise ValidationError(_(str(e)))
finally:
type = None
if 1:
type = 'auto'
else:
type = 'manual'
我认为某些局部变量会创建此错误,但在调试后我知道此if
条件会因此而产生错误type()
。
解决方案
获得该错误的一种方法。
def _something(self, meeting):
if type(meeting.modified_date) != bool:
pass
#rename it
type = "small"
编辑:
try:
if type(meeting.modified_date) != bool:
//code
except Exception as e:
raise ValidationError(_(str(e)))
finally:
#this is local variable and the fix is
record_type = None
if 1:
record_type = 'auto'
else:
record_type = 'manual'
#and later where type is used it should renamed aswell
推荐阅读
- airflow - 气流任务未加入下游任务
- c - 当堆栈变量由函数调用的结果初始化时,是否复制了结构?
- html - ReqactQuill 编辑器在长文本上调整大小
- java - 2 线程轮流打印字母
- javascript - 反应js。数组元素显示更多使用 useHooks useState 显示更少
- java - 在Jenkins从节点中面对javac编译器问题maven项目
- android - 构建失败,因为 react native reanimated 2
- maven - maven - 记录的代码使用模块,但 http://docs.oracle.com/javase/7/docs/api/ 中定义的包位于未命名的模块中
- openssl - OpenSSl 在新版本的验证中返回错误的签名,在旧版本中返回 OK
- reactjs - 如何同步使用包含 setState 的函数