Python类型转换为int的字符串失败

时间:2015-05-15 10:19:16

标签: python integer typeconverter

当我尝试在python(2.7)中从字符串到整数进行简单的类型转换时出现错误:

year = device.time[year_pos:].strip() # This is '1993'
t = int(year) # Throws exception: "UnboundLocalError: local variable 'int' referenced before assignment"

为什么呢? :)

1 个答案:

答案 0 :(得分:2)

UnboundLocalError听起来好像你已经在代码中的某个其他范围内为名称int分配了一些内容(即抛出异常的代码在函数内部,并且您已经使用过int作为在全局代码或其他地方存储变量的名称。有https://developer.amazon.com/public/resources/development-tools/ide-tools/tech-docs/01-setting-up-your-development-environment您可以在这里阅读有关此类问题的更多信息,但对于您的问题,我建议您不要使用内置名称来存储变量。