首页 > 解决方案 > 赋值前引用的局部变量“类型”

问题描述

为什么我会收到此错误?

赋值前引用的局部变量“类型”

代码:

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()

标签: python-3.xodooodoo-13

解决方案


获得该错误的一种方法。

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


推荐阅读