Very Strange Problem

MRAB python at mrabarnett.plus.com
Wed Jul 29 12:53:33 EDT 2009


Ricardo Aráoz wrote:
> MRAB wrote:
>> 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.
> Hi Omer, what he is trying to convey in his rude manner is that you are 
> missing "def" in your function definitions. It is probably a beginners 
> mistake.
> 
> That is :
> from "some_function(index): "
> to "def some_function(index): "
> 
> from "loop(): "
> to "def loop(): "
> 
> I have not tried your code so you should believe him when he states he 
> has actually run the code.
> HTH
> 
Omer says "the print statement afterwards get printed", but the code
provided would have raised a SyntaxError, so omitting the 'def's can't
be the cause of the actual problem reported.



More information about the Python-list mailing list