473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How can this be?

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.
Jul 18 '05 #1
7 1277
r.e.s. wrote:
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

Jul 18 '05 #2
"Paul Prescod" <pa**@prescod.net> 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 = [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.
Jul 18 '05 #3
> 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[0] = 1
.... L = [0]
print L [0] fun(L)
print L [1]

- Josiah
Jul 18 '05 #4
Paul Prescod wrote:
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.
--
I am leeg, for we are many.
http://users.ox.ac.uk/~wadh1342
Jul 18 '05 #5
>>>>> "r" == r e s <r.*@ZZmindspring.com> writes:

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.,
def fun(input): .... 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.
Jul 18 '05 #6
r.e.s. wrote:
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

Jul 18 '05 #7
r.e.s. <r.*@ZZmindspring.com> wrote:
"Paul Prescod" <pa**@prescod.net> 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 = [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

--
Robin Munn
rm***@pobox.com
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Alexander Ross | last post by:
I have a variable ($x) that can have 50 different (string) values. I want to check for 7 of those values and do something based on it ... as I see it I have 2 options: 1) if (($x=="one") ||...
0
by: Dan Foley | last post by:
This script runs fine, but I'd like to know why it's so slow.. Thanks for any help out there on how i can make it faster (it might take up to 5 min to write these 3 export files whith 15 records...
5
by: Lee Redeem | last post by:
Hi there I've created abd uploaded this basic PHP script: <html> <head> <title>PHP Test</title> </head> <body> <H1 align="center">
5
by: christopher vogt | last post by:
Hi, i'm wondering if there is something like $this-> to call a method inside another method of the same class without using the classname in front. I actually use class TEST { function...
6
by: Phil Powell | last post by:
Ok guys, here we go again! SELECT s.nnet_produkt_storrelse_navn FROM nnet_produkt_storrelse s, nnet_produkt_varegruppe v, nnet_storrelse_varegruppe_assoc sv, nnet_produkt p WHERE...
1
by: Michel | last post by:
a site like this http://www.dvdzone2.com/dvd Can you make it in PHP and MySQL within 6 weeks? If so, send me your price 2 a r a (at) p a n d o r a . b e
11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.