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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to