scared about refrences...

B

Bruno Desthuilliers

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

SpreadTooThin

Bruno said:
Nick Vatamaniuc a écrit :
(snip)

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?
 
D

Diez B. Roggisch

SpreadTooThin said:
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
 
F

Fredrik Lundh

SpreadTooThin said:
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>
 
I

Inyeol Lee

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;
... print id(x)
... x = x + 1
... print id(x)
... 1617596
1617608 1617596
1617620
You can assume id() as a memory address. Is it call-by-value?

-- Inyeol Lee
 
D

Dennis Lee Bieber

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

Definitely not...
.... print id(x)
.... x = x + 1
.... print id(x)
....
3302180
3302168
3302180
3302168

Note that the numeric object "2" has an ID (address, in the C
implementation). Note that the name "a" has been bound to the numeric
object, so has the same object ID. Also note that on entry to the
function, "x" has been bound to the same object ID.

BUT, note that on the assignment adding 1, "x" is now bound to a
different object (different ID, different memory address).

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Bruno Desthuilliers

SpreadTooThin a écrit :
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
 
S

SpreadTooThin

Bruno said:
SpreadTooThin a écrit :
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)
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...
 
S

Steve Holden

SpreadTooThin said:
Bruno said:
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...
You are!
a = 2
fn(a)


print a



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
 
G

Gabriel Genellina

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

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

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(foo)=",id(foo)
print "a2=",a2,"id(a2)=",id(a2)
g(foo)
print "after, foo=",foo,"id(foo)=",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
 

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
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top