[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

no pass-values calling?

J. Peng

1/16/2008 3:09:00 AM

Hello,

I saw this statement in Core Python Programming book,

All arguments of function calls are made by reference, meaning that
any changes to these parameters within the function
affect the original objects in the calling function.


Does this mean there is not pass-values calling to a function in
python? only pass-reference calling? Thanks!
19 Answers

Steven D'Aprano

1/16/2008 4:36:00 AM

0

On Wed, 16 Jan 2008 11:09:09 +0800, J. Peng wrote:

> Hello,
>
> I saw this statement in Core Python Programming book,
>
> All arguments of function calls are made by reference, meaning that any
> changes to these parameters within the function affect the original
> objects in the calling function.
>
>
> Does this mean there is not pass-values calling to a function in python?
> only pass-reference calling? Thanks!

No, Python does not use either pass by reference or pass by value. It
uses pass by object. (Sometimes called "pass by object reference".)

See: http://effbot.org/zone/call-by-...

for further details.


--
Steven

Dennis Lee Bieber

1/16/2008 5:45:00 AM

0

On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <peng.kyo@gmail.com>
declaimed the following in comp.lang.python:

>
> All arguments of function calls are made by reference, meaning that

Since all "variable" names in Python are references to objects,
anything accessed using a name is accessed by reference.

def dummy(a1, a2, etc):
pass

dummy("this", something, else)

"links" by effectively doing the equivalent of

a1 = "this"
a2 = something
etc = else

> any changes to these parameters within the function
> affect the original objects in the calling function.
>
The parameter names are local to the function. Any unqualified
assignment to them is also local and switches the name to a different
object.

Assignment to a qualified name (those with . or [] appended) are
"going inside" the object the name refers to and rebinding components of
that object. Of course, this can only work if the object itself is
mutable -- a list, dictionary, module component, or class instance
attribute.

alist = []
anint = 2
astr = "Touch me"

dummy(alist, anint, astr)

"dummy" can only modify the contents of the first argument -- the
integer and string can not be mutated.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Christian Heimes

1/16/2008 5:56:00 AM

0

Dennis Lee Bieber wrote:
> Since all "variable" names in Python are references to objects,
> anything accessed using a name is accessed by reference.

Anybody using the terms variable, reference or call-by-value is most
likely explaining Python the wrong way.

Sorry dude :)

Christian

J. Peng

1/16/2008 5:59:00 AM

0

On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <peng.kyo@gmail.com>
>
> alist = []
> anint = 2
> astr = "Touch me"
>
> dummy(alist, anint, astr)
>
> "dummy" can only modify the contents of the first argument -- the
> integer and string can not be mutated.

Hi,

How to modify the array passed to the function? I tried something like this:

>>> a
[1, 2, 3]
>>> def mytest(x):
.... x=[4,5,6]
....
>>> mytest(a)
>>> a
[1, 2, 3]

As you see, a was not modified.
Thanks!

Steven D'Aprano

1/16/2008 6:30:00 AM

0

On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:

> Hi,
>
> How to modify the array passed to the function? I tried something like
> this:
>
>>>> a
> [1, 2, 3]
>>>> def mytest(x):
> ... x=[4,5,6]


This line does NOT modify the list [1, 2, 3]. What it does is create a
new list, and assign it to the name "x". It doesn't change the existing
list.


If you have not already done this, you should read this:

http://effbot.org/zone/python-o...


Consider this function:

def test(alist):
alist.append(0) # this modifies the existing list
alist = [1, 2, 3] # this changes the name "alist"
return alist


Now try it:

oldlist = [10, 9, 8, 7]
newlist = test(oldlist)


Can you predict what oldlist and newlist will be equal to?

oldlist will be [10, 9, 8, 7, 0] and newlist will be [1, 2, 3]. Do you
see why?




--
Steven

Chris

1/16/2008 6:33:00 AM

0

On Jan 16, 7:59 am, "J. Peng" <peng....@gmail.com> wrote:
> On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
>
> > On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <peng....@gmail.com>
>
> > alist = []
> > anint = 2
> > astr = "Touch me"
>
> > dummy(alist, anint, astr)
>
> > "dummy" can only modify the contents of the first argument -- the
> > integer and string can not be mutated.
>
> Hi,
>
> How to modify the array passed to the function? I tried something like this:
>
> >>> a
> [1, 2, 3]
> >>> def mytest(x):
>
> ... x=[4,5,6]
> ...>>> mytest(a)
> >>> a
>
> [1, 2, 3]
>
> As you see, a was not modified.
> Thanks!

'a' was not modified because you locally assigned a new object with
'x=[4,5,6]'. If you want the new list you created you will have to
return it. You can see how you modify it if you were to use
'x.append()' or 'x.extend()' for eg.

Ben Finney

1/16/2008 6:47:00 AM

0

Christian Heimes <lists@cheimes.de> writes:

> Dennis Lee Bieber wrote:
> > Since all "variable" names in Python are references to objects,
> > anything accessed using a name is accessed by reference.
>
> Anybody using the terms variable, reference or call-by-value is most
> likely explaining Python the wrong way.

The term "reference" is fine, since that's exactly how it works. One
gets at an object via some reference, be it a name or some access into
a container object. When an object has no more references to itself,
it becomes a candidate for garbage collection. And so on.

--
\ "I planted some bird seed. A bird came up. Now I don't know |
`\ what to feed it." -- Steven Wright |
_o__) |
Ben Finney

J. Peng

1/16/2008 6:51:00 AM

0

On Jan 16, 2008 2:30 PM, Steven D'Aprano
<steven@remove.this.cybersource.com.au> wrote:
> On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:
>
> > Hi,
> >
> > How to modify the array passed to the function? I tried something like
> > this:
> >
> >>>> a
> > [1, 2, 3]
> >>>> def mytest(x):
> > ... x=[4,5,6]
>
>
> This line does NOT modify the list [1, 2, 3]. What it does is create a
> new list, and assign it to the name "x". It doesn't change the existing
> list.
>

Sounds strange.
In perl we can modify the variable's value like this way:

$ perl -le '
> $x=123;
> sub test {
> $x=456;
> }
> test;
> print $x '
456

Dennis Lee Bieber

1/16/2008 7:03:00 AM

0

On Wed, 16 Jan 2008 13:59:03 +0800, "J. Peng" <peng.kyo@gmail.com>
declaimed the following in comp.lang.python:


> How to modify the array passed to the function? I tried something like this:
>
> >>> a
> [1, 2, 3]
> >>> def mytest(x):
> ... x=[4,5,6]

x is unqualified (in my terms), so you have just disconnected it
from the original argument and connected it to [4,5,6]

But try

x[1] = [4,5,6]
or
x[:] = [4,5,6]
or
x.append([4,5,6])
or
x.extend([4,5,6])

All of these are qualified "x", meaning "go inside of the object
that 'x' is connected to, and modify the insides}

But note that you can not do this if you pass a string, tuple, or
number -- those are not mutable (while string and tuple can be indexed
to retrieve parts, you can not change parts of them).
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Dennis Lee Bieber

1/16/2008 7:03:00 AM

0

On Wed, 16 Jan 2008 06:55:46 +0100, Christian Heimes <lists@cheimes.de>
declaimed the following in comp.lang.python:


> Anybody using the terms variable, reference or call-by-value is most
> likely explaining Python the wrong way.
>
> Sorry dude :)
>
At least give me credit for putting "variable" in quotes -- to imply
that it isn't the classical "mailbox" variable (fixed address containing
some data)

As far as I'm concerned, there is a pool of objects, and a pool of
names with strings of twine [vs characters <G>] (the references) to
objects. For names, there are, in my mind, unqualified names, and
qualified names. A qualified name is one that "goes inside" an object
and allows changing the inside when it appears on the left of an = (or
in absence of an =); unqualified names on the left of an = move the
name/twine from whatever object to which it had been connected over to
some other object.

a = b #unqualified, "a" is disconnected from whatever, and
#connects it to "b"
a[1] = b #qualified, goes inside "a" to move the connection
#the second element from whatever to "b"
a.it = b #qualified, goes inside of "a" to move the connection
#of the "it" component from whatever to "b"
a.that(b) #goes inside of "a" using some method which might
#connect something to "b"
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/