Very Strange Problem

Dave Angel davea at ieee.org
Wed Jul 29 21:42:39 CEST 2009


Omer Khalid wrote:
> Hi Dave,
>
> Thanks for your reply. I actually didn't cut and paste my code as it was
> dispersed in different places, i typed the logic behind my code in the email
> (and obiviously made some typos, indentations is some thing else) but my
> real code does not have these problems as my application runs fine with out
> errors...
>
> Except that the line where i want to update the value doesn't get updated
> and no exception is thrown. What's surprising for me is that i am doing the
> same thing in hundreds of places in my 3k+ line code but by some reason this
> part doesn't work...
>
> As far as the global variables are concerned, i am using them in other
> places too and didn't see any problems.
>
> I think some thing else is going on here as the statement above and below my
> modified lines get executed.
>
> Is there a way in Python to debug memory address or to see where in memory
> this object is stored, and is there a lock on it or else?
>
> Thanks,
> Omer
>
>
> **************************************
>
>
> On Wed, Jul 29, 2009 at 8:56 PM, Dave Angel <davea at ieee.org> 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!
>>>
>>> Cheers
>>> Omer
>>>
>>>
>>>
>>>       
>> There are four things to fix before the program does anything much at all.
>>  Two places you're missing the def, indentation is inconsistent, and you
>> never actually call either of the functions.   The first three are syntax
>> errors, so presumably your cut/paste in your computer is broken.
>>
>> Once I make those four corrections, I get the following output:
>>
>> Set to 1
>> Set to 1
>> Set to 1
>> Set to 1
>> Set to 1
>>
>> But you never said what you got, nor what you expected.  That's certainly
>> what I'd expect.  And if you make a second call to loop() in your outer
>> code, you get five copies of "Already set to 1"
>>
>> BTW, there are a number of things that could be done better.  The main one
>> I'll point out is that you shouldn't re-use a global variable 'index' as a
>> local with different meaning.  As someone else pointed out, since the global
>> is a constant, making it all uppercase is the convention.
>>
>> DaveA
>>
>>     
(You top-posted, so your ,message is out of sequence.  More and more 
people are doing that in this list.)


++++  ...Except that the line where i want to update the value doesn't 
get updated...

And what makes you think that?  You never answered my question.  What did you expect for output, and what did you get?  I got exactly what I expected, when I ran it.

++++ ...  Is there a way in Python to debug memory address or
++++      to see where in memory this object is stored, and
++++      is there a lock on it or else?

If there really were a bug in the language, you might need such a tool.  
I use Komodo IDE as a debugger, but there was no need in this case.  
Adding a few print statements might clear up your confusion, but since 
you haven't spelled out what it is, I can't suggest where.  How about if 
you just add a    print jobs    at the beginning of some_function() ?  
Then you could see things getting updated perfectly.

DaveA




More information about the Python-list mailing list