473,799 Members | 3,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scared about refrences...

I'm really worried that python may is doing some things I wasn't
expecting... but lets see...

if I pass a list to a function def fn(myList):

and in that function I modify an element in the list, then does the
callers list get modied as well.

def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>>[1,2,3]
[1,0,3]
How can I avoid this? In this case this is a really simplified example
but the effects are the same...
How do I specify or create deep copies of objects that may contain
other objects that may contain other
object that may contain other objects....

Oct 30 '06
28 1233
Nick Vatamaniuc a écrit :
(snip)
In Python all the primitives are copied and all other entities are
references.
Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.
Oct 31 '06 #21

Bruno Desthuilliers wrote:
Nick Vatamaniuc a écrit :
(snip)
In Python all the primitives are copied and all other entities are
references.

Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.
so..
def fn(x):
x = x + 1
print x

a = 2
fn(a)
fn(2)

Wouldn't you say that this is being passed by value rather than by
refrence?

Nov 1 '06 #22
SpreadTooThin schrieb:
Bruno Desthuilliers wrote:
>Nick Vatamaniuc a écrit :
(snip)
>>In Python all the primitives are copied and all other entities are
references.
Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.

so..
def fn(x):
x = x + 1
print x

a = 2
fn(a)
fn(2)

Wouldn't you say that this is being passed by value rather than by
refrence?
It appears so, but it still is a truth - all objects, even the numbers,
are objects. No copying.

Diez
Nov 1 '06 #23
SpreadTooThin wrote:
>Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.

so..
def fn(x):
x = x + 1
print x

a = 2
fn(a)
fn(2)

Wouldn't you say that this is being passed by value rather than by
refrence?
Python uses neither "call by value" nor "call by reference", but that's
irrelevant: the result you're seeing has nothing to do with the calling
model, but with how assignment works. Gabriel already posted this link;
I suggest you read it again:

http://www.effbot.org/zone/python-objects.htm

</F>

Nov 1 '06 #24
On Wed, Nov 01, 2006 at 12:20:36PM -0800, SpreadTooThin wrote:
>
Bruno Desthuilliers wrote:
Nick Vatamaniuc a écrit :
(snip)
In Python all the primitives are copied and all other entities are
references.
Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.

so..
def fn(x):
x = x + 1
print x

a = 2
fn(a)
fn(2)

Wouldn't you say that this is being passed by value rather than by
refrence?
What you're confused with is assignment. Check this example;
>>def f(x):
... print id(x)
... x = x + 1
... print id(x)
...
>>f(1234)
1617596
1617608
>>a = 5678
id(a)
1617596
>>f(a)
1617596
1617620
>>>
You can assume id() as a memory address. Is it call-by-value?

-- Inyeol Lee
Nov 1 '06 #25
SpreadTooThin a écrit :
Bruno Desthuilliers wrote:
>>Nick Vatamaniuc a écrit :
(snip)
>>>In Python all the primitives are copied and all other entities are
references .

Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.


so..
def fn(x):
x = x + 1
print x

a = 2
fn(a)
fn(2)

Wouldn't you say that this is being passed by value rather than by
refrence?
It's not passed by value. when in fn(), the *local* name 'x' is bound to
(IOW:references ) the exact same object you passed to fn(). Then you
rebind this (local) name to *another* object.

def fn((x):
print id(x)
x = x + 1
print id(x)

n = 1256
print id(n)
fn(n)

def fn2(alist):
print id(alist)
alist.append(42 )

mylist = []
print id(mylist)
fn2(mylist)
print mylist
print id(mylist)
There's nothing like "pass by value" or "pass by reference" in Python
(and you'll notice I didn't claimed anything about this - just that the
'argument passing scheme' was the same for all objects).

What we call "variables" in Python are name=>object bindings. When
passing a "variable" to a function, the reference to the objet is bound
to the name of the argument in the function's namespace. So the *name*
is local to the function (hence rebinding the name to another objet
doesn't impact the name=>object binding in the caller's namespace), but
this name really refers to the same object (Python doesn't copy anything
unless explicitely told to do so).

HTH
Nov 2 '06 #26

Bruno Desthuilliers wrote:
SpreadTooThin a écrit :
Bruno Desthuilliers wrote:
>Nick Vatamaniuc a écrit :
(snip)

In Python all the primitives are copied and all other entities are
references.

Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.

so..
def fn(x):
x = x + 1
print x

a = 2
fn(a)
fn(2)

Wouldn't you say that this is being passed by value rather than by
refrence?

It's not passed by value. when in fn(), the *local* name 'x' is bound to
(IOW:references ) the exact same object you passed to fn(). Then you
rebind this (local) name to *another* object.

def fn((x):
print id(x)
x = x + 1
print id(x)

n = 1256
print id(n)
fn(n)

def fn2(alist):
print id(alist)
alist.append(42 )

mylist = []
print id(mylist)
fn2(mylist)
print mylist
print id(mylist)
There's nothing like "pass by value" or "pass by reference" in Python
(and you'll notice I didn't claimed anything about this - just that the
'argument passing scheme' was the same for all objects).

What we call "variables" in Python are name=>object bindings. When
passing a "variable" to a function, the reference to the objet is bound
to the name of the argument in the function's namespace. So the *name*
is local to the function (hence rebinding the name to another objet
doesn't impact the name=>object binding in the caller's namespace), but
this name really refers to the same object (Python doesn't copy anything
unless explicitely told to do so).

HTH

I realize I may be beating a dead horse here... but...

a = 2
fn(a)
>>3
print a
>>2
So in some cases the it is safe to assume that your variables to
function will not
change in other cases it is not.. but they are all the same...

Nov 2 '06 #27
SpreadTooThin wrote:
Bruno Desthuilliers wrote:
>>SpreadTooTh in a écrit :
>>>Bruno Desthuilliers wrote:
Nick Vatamaniuc a écrit :
(snip)
>In Python all the primitives are copied and all other entities are
>references .

Plain wrong. There's no "primitives " (ie : primitive data types) in
Python, only objects. And they all get passed the same way.
so..
def fn(x):
x = x + 1
print x

a = 2
fn(a)
fn(2)

Wouldn't you say that this is being passed by value rather than by
refrence?

It's not passed by value. when in fn(), the *local* name 'x' is bound to
(IOW:referenc es) the exact same object you passed to fn(). Then you
rebind this (local) name to *another* object.

def fn((x):
print id(x)
x = x + 1
print id(x)

n = 1256
print id(n)
fn(n)

def fn2(alist):
print id(alist)
alist.append(42 )

mylist = []
print id(mylist)
fn2(mylist)
print mylist
print id(mylist)
There's nothing like "pass by value" or "pass by reference" in Python
(and you'll notice I didn't claimed anything about this - just that the
'argument passing scheme' was the same for all objects).

What we call "variables" in Python are name=>object bindings. When
passing a "variable" to a function, the reference to the objet is bound
to the name of the argument in the function's namespace. So the *name*
is local to the function (hence rebinding the name to another objet
doesn't impact the name=>object binding in the caller's namespace), but
this name really refers to the same object (Python doesn't copy anything
unless explicitely told to do so).

HTH

I realize I may be beating a dead horse here... but...
You are!
a = 2
fn(a)
>>>>3

print a
>>>>2


So in some cases the it is safe to assume that your variables to
function will not
change in other cases it is not.. but they are all the same...
Mutable values can be mutated. Immutable ones can't.

Please stop worrying and start enjoying Python. Once you have started
cutting code you will wonder why you ever bothered to spend so much time
on this issue.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 2 '06 #28
At Thursday 2/11/2006 19:23, SpreadTooThin wrote:
>I realize I may be beating a dead horse here... but...

def fn(x):
x = x + 1
print x
a = 2
fn(a)
>3
print a
>2

So in some cases the it is safe to assume that your variables to
function will not
change in other cases it is not.. but they are all the same...
Forget about variables!
In Python you have objects. Objects don't have a name, but you may
use a name to refer to them.
In the code above, `a` is not a variable, neither an object; it's
just a name referring to an object (a name bound to an object); such
object is an instance of type int, with value 2, to be precise.
fn(a) calls function fn [in fact, "the object bound to the name fn"]
passing an object -the object referenced by the name `a`- as its only argument.
Inside function fn, it gets a single argument bound to the name x.
You do some math and bind the resulting object to the local name `x`.
Now x refers to another instance of type int, with value 3. The
original int object is not modified - and never could have been,
since int's are immutable, you cant change them (a 2 will always be a
2: if you get a 3, it's ANOTHER object).
After function fn finishes, execution continues with the `print a`
statement. Nobody has changed the object pointed by the name `a`, so
it's still the int with value 2, and that's what you get.

All objects have identity. You can test identity using the `is`
operator: `a is b` returns True iff both objects are identical (both
are "the same object" just being referred by another name). The
function id() returns the object identity.

Some objects (by example, containers like lists and dicts) are
mutable. That means that you can change it's value, but the remain
being the same object. By example:

--- begin test.py ---
def g(x):
print 'x[1], in:', x[1]
x[1] = 10
print 'x[1], out:', x[1]

a1=1
a2=2
a3=3
foo=[a1,a2,a3]
print "before, foo=",foo,"id(f oo)=",id(foo)
print "a2=",a2,"id(a2 )=",id(a2)
g(foo)
print "after, foo=",foo,"id(f oo)=",id(foo)
print "a2=",a2,"id(a2 )=",id(a2)
--- end test.py ---
Output:

before, foo= [1, 2, 3] id(foo)= 12350728
a2= 2 id(a2)= 11163404
x[1], in: 2
x[1], out: 10
after, foo= [1, 10, 3] id(foo)= 12350728
a2= 2 id(a2)= 11163404

Function g modifies the second "slot" in the list - it does not
modify the object already in that place, it just makes the second
item in the list to refer to another object (the int 10). The
previous reference (the int 2) is lost.
Anyway, the list remains "the same" (look at id(foo)).
The name a2 still refers to the int 2 (because nobody changed that).

It's really easy once you get it - and then, just enjoy writing Python code!
--
Gabriel Genellina
Softlab SRL

_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 2 '06 #29

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

Similar topics

0
1087
by: Lawrence Oluyede | last post by:
http://www.cardboard.nu/archives/000118.html I think that the guy (Alan Green) is very scared :) I read also a funny (but clever) comment to that post (the first one): " True, True. In fact, we should boycott Java, too. C is the way to go! (Or Assembler? Or just write the binary code by hand?) "
3
1373
by: Lawrence Oluyede | last post by:
http://www.cardboard.nu/archives/000118.html I think that the guy (Alan Green) is very scared :) I read also a funny (but clever) comment to that post (the first one): " True, True. In fact, we should boycott Java, too. C is the way to go! (Or Assembler? Or just write the binary code by hand?) "
0
1093
by: sonic_soul | last post by:
hi i have a solution with 22 loaded projects. 1 of those projects is a web app, and this web app is using functionality from the other 21 projects which are simple dll's in my web project i need to add a refrence to other projects so that the dll's are automatically copied to my web app/bin directory. i can do that for all but a couple of them..
0
900
by: Josh | last post by:
Hi Guys, I have lots of web refrences connecting to different sites and i need to be able to dynamically call the web refrence based on output from a database. At the moment i am doing the following; //send execution command to the client to update the database wrAmp.dbExecutor dbe = new wrAmp.dbExecutor(); bool barf = dbe.updateDatabase();
3
1569
by: Afshar | last post by:
Hi, I am writing a single .cs in IDE that uses some refrences that I have created previously as dll. I compile this refrences with /r switch in csc.exe with no problem. IntelliSense works with cs commands and syntaxes. But the problem is It doesn't know my own refrences and so doesn't show my class members. What can I do with with these? Please Help! Regards,
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.