user3305609
user3305609

Reputation: 155

Python typecasting string to int fails

I get an error when I try to do a simple typecasting from string to integer in python (2.7):

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

Why? :)

Upvotes: 2

Views: 350

Answers (1)

wildwilhelm
wildwilhelm

Reputation: 5019

The UnboundLocalError sounds as if you've assigned something to the name int in some other scope in your code (i.e., the code throwing the exception is inside a function, and you've used int as a name to store a variable in global code, or somewhere else). There's a blog post about UnboundLocalError where you can read more about this kind of problem, but for your issue, I'd just recommend not using builtin names to store variables.

Upvotes: 2

Related Questions