当我尝试在 python (2.7) 中进行从字符串到整数的简单类型转换时出现错误:
year = device.time[year_pos:].strip() # This is '1993'
t = int(year) # Throws exception: "UnboundLocalError: local variable 'int' referenced before assignment"
为什么? :)
请您参考如下方法:
UnboundLocalError 听起来好像您在代码的其他范围内为名称 int 分配了一些东西(即,抛出异常的代码在函数内部,并且您使用了 int 作为名称来将变量存储在全局代码或其他地方)。有 a blog post about UnboundLocalError您可以在哪里阅读有关此类问题的更多信息,但对于您的问题,我只建议不要使用内置名称来存储变量。
