443,965 Members | 1,779 Online Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

# How can this be?

 P: n/a My problem is this ... Module A contains the definition of a function f. Module B contains: from A import * L =  print L x = f(L, 'a data string') print L When Module B is imported/reloaded (in pythonwin), the result printed in the interactive window is   How can the list L have gotten changed?? Without my posting the function def (which is very lengthy), can anyone possibly give me a clue about what might be a likely cause of this behavior? (It *is* irregular, isn't it? ?) Thanks for any help. --r.e.s. Jul 18 '05 #1
7 Replies

 P: n/a r.e.s. wrote: My problem is this ... Module A contains the definition of a function f. Module B contains: from A import * L =  print L x = f(L, 'a data string') print L ... How can the list L have gotten changed?? You sent a reference to L into the function. The function probably mutated it. If you wish the function to work with a copy, try this: from A import * L =  print L x = f(L[:], 'a data string') print L This is normal and often useful behaviour. Paul Prescod Jul 18 '05 #2

 P: n/a "Paul Prescod" wrote ... You sent a reference to L into the function. The function probably mutated it. If you wish the function to work with a copy, try this: from A import * L =  print L x = f(L[:], 'a data string') print L This is normal and often useful behaviour. I can see that it would be -- Thanks for answering a beginner's question. I notice also the following: def f(L): L = 1 def g(L): L =  L1 = , L2 =  f(L1), g(L2) .... L1 gets mutated, but not L2 (even though both references were passed). It's probably explained in the tutorial -- which I'm now going to re-read. --r.e.s. Jul 18 '05 #3

 P: n/a > Without my posting the function def (which is very lengthy), can anyone possibly give me a clue about what might be a likely cause of this behavior? (It *is* irregular, isn't it? ?) Your function is changing it: def fun(input): .... input = 1 .... L =  print L  fun(L) print L  - Josiah Jul 18 '05 #4

 P: n/a Paul Prescod wrote: If you wish the function to work with a copy, try this: from A import * L =  print L x = f(L[:], 'a data string') print L Heh, and there was me doing: def copylist(source): dest=[] for i in source: dest.append(i) return dest a few tens of thousands of times a minute. I guess using slices is faster :-) Cheers, leeg. -- I am leeg, for we are many. http://users.ox.ac.uk/~wadh1342 Jul 18 '05 #5

 P: n/a >>>>> "r" == r e s writes: r> My problem is this ... Module A contains the definition of a r> function f. Module B contains: r> from A import * L =  print L x = f(L, 'a data string') print L r> When Module B is imported/reloaded (in pythonwin), the result printed r> in the interactive window is r>   r> How can the list L have gotten changed?? r> Without my posting the function def (which is very lengthy), can r> anyone possibly give me a clue about what might be a likely cause of r> this behavior? (It *is* irregular, isn't it? ?) Not quite. The function cannot change the binding that L is pointing to that particular list object, and this is the case. E.g., def fun(input): .... input = 1 .... input =  .... L =  print L  print id(L) 1075979404 fun(L) print L  print id(L) 1075979404 In other words, the function changes the content of L, which is allowed (as long as the object provide some interface for you to do so). But it cannot change L to point to another object (here, ). Regards, Isaac. Jul 18 '05 #6

 P: n/a r.e.s. wrote: My problem is this ... Module A contains the definition of a function f. Module B contains: from A import * L =  print L x = f(L, 'a data string') print L When Module B is imported/reloaded (in pythonwin), the result printed in the interactive window is   How can the list L have gotten changed?? Because you changed it in f() ? Without my posting the function def (which is very lengthy), can anyone possibly give me a clue about what might be a likely cause of this behavior? Sorry, we are not psychic. The only assumption I can make is that f() changes the content of the first item in L. (It *is* irregular, isn't it? ?) How could we tell ? If f() modifies L, it seems pretty regular that L is modified. Bruno Jul 18 '05 #7

 P: n/a r.e.s. wrote: "Paul Prescod" wrote ... You sent a reference to L into the function. The function probably mutated it. If you wish the function to work with a copy, try this: from A import * L =  print L x = f(L[:], 'a data string') print L This is normal and often useful behaviour. I can see that it would be -- Thanks for answering a beginner's question. I notice also the following: def f(L): L = 1 def g(L): L =  L1 = , L2 =  f(L1), g(L2) ... L1 gets mutated, but not L2 (even though both references were passed). It's probably explained in the tutorial -- which I'm now going to re-read. Fredrik Lundh's short article on Python objects (which starts with the helpful advice "Reset your brain") is also useful as a starting point: http://effbot.org/zone/python-objects.htm -- Robin Munn rm***@pobox.com Jul 18 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion. 