How can this be?

R

r.e.s.

My problem is this ...

Module A contains the definition of a function f.
Module B contains:

from A import *
L = [0]
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

[0]
[1]

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.
 
P

Paul Prescod

r.e.s. said:
My problem is this ...

Module A contains the definition of a function f.
Module B contains:

from A import *
L = [0]
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 = [0]
print L
x = f(L[:], 'a data string')
print L

This is normal and often useful behaviour.

Paul Prescod
 
R

r.e.s.

Paul Prescod said:
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 = [0]
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[0] = 1

def g(L):
L = [1]

L1 = [0], L2 = [0]

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.
 
I

Isaac To

r> My problem is this ... Module A contains the definition of a
r> function f. Module B contains:

r> from A import * L = [0] 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> [0] [1]

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.,
.... input[0] = 1
.... input = [2]
....
L = [0]
print L [0]
print id(L) 1075979404
fun(L)
print L [1]
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, [2]).

Regards,
Isaac.
 
L

leeg

Paul said:
If you wish the function to work with a copy, try this:

from A import *
L = [0]
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.
 
B

Bruno Desthuilliers

r.e.s. said:
My problem is this ...

Module A contains the definition of a function f.
Module B contains:

from A import *
L = [0]
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

[0]
[1]

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
 
R

Robin Munn

r.e.s. said:
Paul Prescod said:
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 = [0]
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[0] = 1

def g(L):
L = [1]

L1 = [0], L2 = [0]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top