python, dictionary and int error
-
23-08-2019 - |
Question
I have a very frustrating python problem. In this code
fixedKeyStringInAVar = "SomeKey"
def myFunc(a, b):
global sleepTime
global fixedKeyStringInAVar
varMe=int("15")
sleepTime[fixedKeyStringInAVar] = varMe*60*1000
#more code
Now this works. BUT sometimes when I run this function I get
TypeError: 'int' object does not support item assignment
It is extremely annoying since I tried several test cases and could not reproduce the error, yet it happens very often when I run the full code. The code reads data from a db, access sites, etc. so its hard for me to go through the data since it reads from several sources and depends on 3rd party input that changes (websites).
What could this error be?
Solution
Don't use
global
keyword in a function unless you'd like to change binding of a global name.Search for '
sleepTime =
' in your code. You are binding an int object to thesleepTime
name at some point in your program.
OTHER TIPS
Your sleepTime is a global variable. It could be changed to be an int at some point in your program.
The item assignment is the "foo[bar] = baz" construction in your function.