no pass-values calling?

J

J. Peng

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

Steven D'Aprano

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-object.htm

for further details.
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
C

Christian Heimes

Dennis said:
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

J. Peng

On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <[email protected]>

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:
.... x=[4,5,6]
....[1, 2, 3]

As you see, a was not modified.
Thanks!
 
S

Steven D'Aprano

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-objects.htm


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

Chris

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)
[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.
 
B

Ben Finney

Christian Heimes said:
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.
 
J

J. Peng

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
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
C

Christian Heimes

Ben said:
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.

Thanks you, but I know exactly how Python works. I'm actually developing
CPython and PythonDotNET. While your description of Python's memory
management is technically, it's just an implementation detail of the
CPython implementation. Jython and IronPython are using different
approaches for GC.

Anyway your message doesn't help a newbie and it gives most certainly
the wrong impression. You are using words that have a different meaning
in other languages. If you explain Python w/o the words variable,
pointer, reference or call-by-value you have a much better chance to
explain it right. Trust me :)

Christian
 
J

J. Peng

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]

Ok, thanks.
But there is a following question,when we say,

we create a list.then we say,

we create a new list and assign it to x for future use.
How to destroy the before list [1,2,3]? does python destroy it automatically?
 
B

Ben Finney

Christian Heimes said:
Thanks you, but I know exactly how Python works. I'm actually
developing CPython and PythonDotNET.

Uh, okay. I didn't ask for you to flash your credentials, but if that
is significant to you, be my guest.
Anyway your message doesn't help a newbie and it gives most
certainly the wrong impression. You are using words that have a
different meaning in other languages. If you explain Python w/o the
words variable, pointer, reference or call-by-value you have a much
better chance to explain it right. Trust me :)

I've done my share of explaining of Python to people, and found
"reference" to be exactly the right term to help the newbie understand
what's happening and what they should expect.

I agree with you on "variable", "pointer", and "call-by-value". Those
don't describe how Python works, and thus only confuse the matter.
Thus I avoid them, and correct newbies who appear to be getting
confused because of those existing concepts.
 
S

Steven D'Aprano

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.

The term "reference" *by itself* is fine, so long as there is no
possibility of confusion with the very common concept of "call by
reference" (or "pass by reference").

But since the Original Poster himself raised the question of whether
Python is call by reference or call by value, that should be a great big
warning flag to avoid the term "reference" until he's reset his brain.
Python is not C, and if you talk about Python using C terminology without
making it absolutely crystal clear that the semantics of that terminology
is different in Python, then you'll just reinforce the O.P.'s
misunderstandings.

Python might have "references" in the generic sense, but in the specific
sense that it is understood by most people with C/Pascal/Java/Perl
experience, Python does not.
 
B

Bruno Desthuilliers

J. Peng a écrit :
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

This is Python, not Perl. Please follow the links provided by Steven and
read carefully.
 
T

Torsten Bronger

Hallöchen!

J. Peng said:
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

But here, it is a global name rather than a parameter. However, I
don't speak Perl.

Tschö,
Torsten.
 
M

Mel

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

$ perl -le '
456

Not all that strange. The Python equivalent is

x=123
sub test()
global x
x=456
test()
print x

Python assignment works by binding an object with a name in a
namespace. I suspect that perl does something similar, and the
differences are in the rules about which namespace to use.

Mel.
 
D

Dennis Lee Bieber

a
[1, 2, 3]
def mytest(x):
... x=[4,5,6]
said:
Ok, thanks.
But there is a following question,when we say,

we create a list.then we say,

we create a new list and assign it to x for future use.
How to destroy the before list [1,2,3]? does python destroy it automatically?

In this example -- no... Because the name "a" is still attached to
it. In general though, if there is no path to access the object, then
Python will dispose of it.
--
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/
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top