views:

331

answers:

2

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 fuction i get

TypeError: 'int' object does not support item assignment

It is extremely annoying since i trid 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?

+1  A: 

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.

+6  A: 
  1. Don't use global keyword in a function unless you'd like to change binding of a global name.

  2. Search for 'sleepTime =' in your code. You are binding an int object to the sleepTime name at some point in your program.

J.F. Sebastian
yep, i did sleepTime = int at one place. The problem was... while moving code i happen to have the same variable name. I changed it to waitTime and left the global to sleepTime.
acidzombie24