Very Strange Problem

MRAB python at mrabarnett.plus.com
Wed Jul 29 11:53:47 EDT 2009


Omer Khalid wrote:
> Hi,
> 
> I am having a very strange problem with modifying a variable in a list 
> in my program. Here is the code:
> 
> # a list that contains dictionary objects
> jobs = []
> 
> index=5
> for each in range(index):
>          jobs.append({'v':0})
> 
> some_function(index):
>        if jobs[index]['v'] == 0:
>                    # set it to 1
>                    jobs[index]['v'] = 1
>                    print "Set to 1"
>       else:
>                    print "Already set to 1"
> 
> loop():
>         index=0
>         for each in range(len(jobs)):
>                  some_function(index)
>                  index +=1
> 
> 
> Apparently, the jobs[index]['v'] never get updated in the some_function 
> but the print statement afterwards get printed...
> 
> What's really surprising is that there are no errors or exceptions and 
> my my program runs in a single thread...so i have been unable to explain 
> this behavior.
> 
> Any insight would be much appreciated!
> 
Well, when I insert the missing 'def's in the function definitions, it
works for me.



More information about the Python-list mailing list