Re: Very Strange Problem

2009-07-30 Thread Simon Forman
On Wed, Jul 29, 2009 at 3:10 PM, Omer Khalidomer.kha...@cern.ch 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

Please, do not do  that.  It's very difficult to debug code that
hasn't been seen.  The code you posted has numerous problems (that
likely have nothing to do with your actual problem.)

If you're going to post code, try to recreate the issue with a small
runnable script.  If you can't do that, as it sounds like it would be
difficult in this case as your code is dispersed in different places,
at least post the relevant portions of the actual code.  Don't
re-type.

 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.

If the statements above and below that line(s) are executed, then that
line is certainly being executed as well.

Try introducing some additional print statements to verify your mental
model of what's happening:

# set it to 1
print jobs
print jobs[index]
print jobs[index]['v']
jobs[index]['v'] = 1
print jobs
print jobs[index]
print jobs[index]['v']
print Set to 1

 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?

You are barking up the wrong tree.  Somewhere in your code you're
doing something silly that's causing your issue.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very Strange Problem

2009-07-29 Thread MRAB

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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Very Strange Problem

2009-07-29 Thread MRAB

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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Very Strange Problem

2009-07-29 Thread Dave Angel

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Very Strange Problem

2009-07-29 Thread Omer Khalid
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 da...@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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very Strange Problem

2009-07-29 Thread Terry Reedy

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:


To me, this sentence clearly implies that the code that follows is the 
code that had the problem. Since the posted code cannot run, it clearly 
is not. People should test code to be posted before posting unless they 
clearly label it as 'untested'. Original posters, of course, should run 
the code first.


tjr

--
http://mail.python.org/mailman/listinfo/python-list


Re: Very Strange Problem

2009-07-29 Thread Dave Angel

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 da...@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 aprint jobsat the beginning of some_function() ?  
Then you could see things getting updated perfectly.


DaveA

--
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-10 Thread conor . robinson
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s).  Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-10 Thread conor . robinson
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s).  Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-10 Thread conor . robinson
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s).  Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-10 Thread conor . robinson
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s).  Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-10 Thread conor . robinson
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s).  Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-08 Thread Fredrik Lundh
John Zenger wrote:

 Your list probably contains several references to the same object,
 instead of several different objects.  This happens often when you use a
 technique like:

 list = [ object ] * 100

 ..because although this does make copies when object is an integer, it
 just makes references in other cases.

it always creates new references.

the only thing that distinguishes immutable objects (like integers) from
mutable objects (like lists) is that integers don't have any methods that
let you modify their contents.

there's no this object is mutable flag inside the object, and there's no
code in the list multiply operation, or anywhere else, that looks for such
a flag.

/F



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-08 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
 The Problem (very basic, but strange):
 
 I have a list holding a population of objects, each object has 5 vars
 and appropriate funtions to get or modify the vars. 

Which are probably not necessary:
http://dirtsimple.org/2004/12/python-is-not-java.html

(in short: Python as a mechanism named properties that allow you to 
gateway attribute access thru hiddens getter and setter).

 When objects in
 the list have identical vars (like all = 5 for var a and all = 10 for
 var b across all vars and objects) and i change
 
 self.mylist[i].change_var_a(5)
 
 to a new value, in this case var a in object i to 5, now all vars of
 type a in all objects in my list are changed to 5 instead of just var
 a in object mylist[i], which is my goal.

(snip)
 
 What is python doing? Am I missing something? Any ideas at all would be
 wonderful?
 

It would have helped if you had posted your code. Anyway, at least two 
things could lead to the behaviour you describe:

1/ you have class attributes instead of instance attributes, ie:

class MyClass(object):
   # this attribute belongs to the class
   class_attrib = 42

   def __init__(self, instance_attrib):
 # this attribute belongs to the instance
 self.instance_attrib = instance_attrib

2/ you in fact have in your list multiple references to the same instance.

My 2 cents
-- 
http://mail.python.org/mailman/listinfo/python-list


very strange problem in 2.4

2006-04-07 Thread conor . robinson
The Problem (very basic, but strange):

I have a list holding a population of objects, each object has 5 vars
and appropriate funtions to get or modify the vars.  When objects in
the list have identical vars (like all = 5 for var a and all = 10 for
var b across all vars and objects) and i change

self.mylist[i].change_var_a(5)

to a new value, in this case var a in object i to 5, now all vars of
type a in all objects in my list are changed to 5 instead of just var
a in object mylist[i], which is my goal.

if i print self.mylist[i].return_var_a() right after I change var a
in object i, I get the correct change, however when i print out the
whole list, the last change to var a in the last object modified
takes over for all objects in the list.

note: all the vars not being modified must be the same across all
objects in the list, the var being modified need not be the same as the
one before it in the list (but will be once just one of the identical
object are changed).  The value changed in the last object var modified
takes over for all object vars making them exactly identical.

If, for example, half the list has objects with random vars init.
and the other half is identical, as above, and I perform the same
operation, as above, to one of the identical var objects

self.mylist[i].change_var_a(5) (to an object that has identicals in the
list)

all the identicals are changed in the same way as above, however the
objects that have different var values are unchanged.



What is python doing? Am I missing something? Any ideas at all would be
wonderful?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-07 Thread John Zenger
Your list probably contains several references to the same object, 
instead of several different objects.  This happens often when you use a 
technique like:

list = [ object ] * 100

..because although this does make copies when object is an integer, it 
just makes references in other cases.

[EMAIL PROTECTED] wrote:
 The Problem (very basic, but strange):
 
 I have a list holding a population of objects, each object has 5 vars
 and appropriate funtions to get or modify the vars.  When objects in
 the list have identical vars (like all = 5 for var a and all = 10 for
 var b across all vars and objects) and i change
 
 self.mylist[i].change_var_a(5)
 
 to a new value, in this case var a in object i to 5, now all vars of
 type a in all objects in my list are changed to 5 instead of just var
 a in object mylist[i], which is my goal.
 
 if i print self.mylist[i].return_var_a() right after I change var a
 in object i, I get the correct change, however when i print out the
 whole list, the last change to var a in the last object modified
 takes over for all objects in the list.
 
 note: all the vars not being modified must be the same across all
 objects in the list, the var being modified need not be the same as the
 one before it in the list (but will be once just one of the identical
 object are changed).  The value changed in the last object var modified
 takes over for all object vars making them exactly identical.
 
 If, for example, half the list has objects with random vars init.
 and the other half is identical, as above, and I perform the same
 operation, as above, to one of the identical var objects
 
 self.mylist[i].change_var_a(5) (to an object that has identicals in the
 list)
 
 all the identicals are changed in the same way as above, however the
 objects that have different var values are unchanged.
 
 
 
 What is python doing? Am I missing something? Any ideas at all would be
 wonderful?
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-07 Thread Steven D'Aprano
On Fri, 07 Apr 2006 21:18:12 -0400, John Zenger wrote:

 Your list probably contains several references to the same object, 
 instead of several different objects.  This happens often when you use a 
 technique like:
 
 list = [ object ] * 100
 
 ..because although this does make copies when object is an integer, it 
 just makes references in other cases.

Wrong. It always makes references.

 L = [1]*3
 id(L[0]), id(L[1]), id(L[2])
(155972920, 155972920, 155972920)

This isn't a caching issue either, it also happens for objects which
aren't cached:

 x = 4591034.56472
 y = 4591034.56472
 x == y
True
 x is y
False
 L = [x]*3
 x is L[0] is L[1] is L[2]
True
 y is L[0]
False



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-07 Thread Ben Cartwright
John Zenger wrote:
 Your list probably contains several references to the same object,
 instead of several different objects.  This happens often when you use a
 technique like:

 list = [ object ] * 100

This is most likely what's going on.  To the OP: please post the
relevant code, including how you create mylist and the definitions of
change_var_a and return_var_a.  I suspect you're doing something like
this:

 \
class C(object):
def __init__(self, x):
self.x = x
def __repr__(self):
return 'C(%r)' % self.x

 mylist = [C(0)]*3 + [C(1)]*3
 mylist
[C(0), C(0), C(0), C(1), C(1), C(1)]
 mylist[0].x = 2
[C(2), C(2), C(2), C(1), C(1), C(1)]

When you should do something like:

 mylist = [C(0) for i in range(3)] + [C(1) for i in range(3)]
[C(0), C(0), C(0), C(1), C(1), C(1)]
 mylist[0].x = 2
[C(2), C(0), C(0), C(1), C(1), C(1)]

--Ben

-- 
http://mail.python.org/mailman/listinfo/python-list