Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

no pass-values calling?

0 views
Skip to first unread message

J. Peng

unread,
Jan 15, 2008, 10:09:09 PM1/15/08
to pytho...@python.org
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!

Steven D'Aprano

unread,
Jan 15, 2008, 11:36:27 PM1/15/08
to

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.


--
Steven

Message has been deleted

Christian Heimes

unread,
Jan 16, 2008, 12:55:46 AM1/16/08
to pytho...@python.org
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

unread,
Jan 16, 2008, 12:59:03 AM1/16/08
to pytho...@python.org
On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <wlf...@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!

Steven D'Aprano

unread,
Jan 16, 2008, 1:30:27 AM1/16/08
to
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-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?


--
Steven

Chris

unread,
Jan 16, 2008, 1:33:03 AM1/16/08
to
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

unread,
Jan 16, 2008, 1:46:39 AM1/16/08
to
Christian Heimes <li...@cheimes.de> writes:

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

unread,
Jan 16, 2008, 1:51:10 AM1/16/08
to pytho...@python.org
On Jan 16, 2008 2:30 PM, Steven D'Aprano

<ste...@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

Message has been deleted
Message has been deleted

Christian Heimes

unread,
Jan 16, 2008, 2:11:05 AM1/16/08
to pytho...@python.org
Ben Finney wrote:
> 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. Peng

unread,
Jan 16, 2008, 2:18:52 AM1/16/08
to Dennis Lee Bieber, pytho...@python.org
On Jan 16, 2008 3:03 PM, Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:
> On Wed, 16 Jan 2008 13:59:03 +0800, "J. Peng" <peng...@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]
>

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

>>> x=[1,2,3]

we create a list.then we say,

>>> x=[4,5,6]

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?

Hrvoje Niksic

unread,
Jan 16, 2008, 2:22:41 AM1/16/08
to
"J. Peng" <peng...@gmail.com> writes:

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

Yes, Python detects that the older list is no longer in use (if that
is indeed the case), and destroys it automatically. If you're curious
how that works, take a look at
http://en.wikipedia.org/wiki/Reference_counting

Ben Finney

unread,
Jan 16, 2008, 3:11:53 AM1/16/08
to
Christian Heimes <li...@cheimes.de> writes:

> Ben Finney wrote:
> > 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.

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.

--
\ "I don't accept the currently fashionable assertion that any |
`\ view is automatically as worthy of respect as any equal and |
_o__) opposite view." -- Douglas Adams |
Ben Finney

Steven D'Aprano

unread,
Jan 16, 2008, 6:33:47 AM1/16/08
to
On Wed, 16 Jan 2008 17:46:39 +1100, Ben Finney wrote:

> Christian Heimes <li...@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.

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.

--
Steven

Bruno Desthuilliers

unread,
Jan 16, 2008, 6:51:22 AM1/16/08
to
J. Peng a écrit :

> On Jan 16, 2008 2:30 PM, Steven D'Aprano
> <ste...@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

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

Torsten Bronger

unread,
Jan 16, 2008, 7:01:57 AM1/16/08
to
Hallöchen!

J. Peng writes:

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

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: bro...@jabber.org
(See http://ime.webhop.org for further contact info.)

Neil Cerutti

unread,
Jan 16, 2008, 7:21:07 AM1/16/08
to pytho...@python.org
On Jan 15, 2008 10:09 PM, J. Peng <peng...@gmail.com> 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.

Yes, that's generally correct. But you must be careful about what is
meant by "changes to parameters". Assigning a new value to a parameter
name (inside the function, a parameter is just a local variable) does
not change the original object--it only rebinds the local variable to
a new object.

In the following function, a is rebound with an assignment statement,
while b is mutated, i.e., changed, with an assignment statement.

def f(a, b):
a = 12
b.value = 14

Argument a will never be changed, while argument b will be. Python's
argument passing semantics are extremely simple. It's the assignment
statement that's tricky: some assignments mutate/change objects, and
some only rebind names.

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

Neither is quite true. Values are passed by binding parameter names to
their corresponding arguments. This is similar to pass-by-reference in
some cases (when the argument is mutated) but not in others (when the
argument is not mutated). Thinking of it as pass-by-reference may help
you to understand it, but bear in mind that Python's "references" may
be rebound to new objects, which is quite different from the usual
behavior of references.

--
Neil Cerutti <mr.cerut...@gmail.com>

cokof...@gmail.com

unread,
Jan 16, 2008, 7:58:58 AM1/16/08
to
On Jan 16, 1:21 pm, "Neil Cerutti" <mr.ceru...@gmail.com> wrote:
> Neil Cerutti <mr.cerutti+pyt...@gmail.com>

So basically the scope is the reason for confusion a lot of the time?

def some_name():
alist = [5]
bint = 5
cstring = '5'
ddictionary = {0:5}

def other_name(alist, bint, cstring, ddictionary):
alist = 4
bint = 4
cstring = '4'
ddictionary = 4
print "other_name:",
print alist, bint, cstring, ddictionary

def another_name(alist, bint, cstring, ddictionary):
alist[0] = 3
# bint cannot be changed it is immutable
# cstring cannot be changed it is immutable
ddictionary[0] = 3
print "another_name:",
print alist, bint, cstring, ddictionary

another_name(alist, bint, cstring, ddictionary)
other_name(alist, bint, cstring, ddictionary)
print "our entries are now:",
print alist, bint, cstring, ddictionary

Neil Cerutti

unread,
Jan 16, 2008, 8:14:23 AM1/16/08
to pytho...@python.org
On Jan 16, 2008 7:58 AM, <cokof...@gmail.com> wrote:
> On Jan 16, 1:21 pm, "Neil Cerutti" <mr.ceru...@gmail.com> wrote:
> > In the following function, a is rebound with an assignment statement,
> > while b is mutated, i.e., changed, with an assignment statement.
> >
> > def f(a, b):
> > a = 12
> > b.value = 14
> >
> > Argument a will never be changed, while argument b will be. Python's
> > argument passing semantics are extremely simple. It's the assignment
> > statement that's tricky: some assignments mutate/change objects, and
> > some only rebind names.
>
> So basically the scope is the reason for confusion a lot of the time?

No, my hypothesis is that Python's assignment statement semantics are
the tricky part--once you understand them, the utter simplicity of
Python's argument passing semantics will be evident.

--
Neil Cerutti <mr.cerut...@gmail.com>

cokof...@gmail.com

unread,
Jan 16, 2008, 8:42:44 AM1/16/08
to
>
> No, my hypothesis is that Python's assignment statement semantics are
> the tricky part--once you understand them, the utter simplicity of
> Python's argument passing semantics will be evident.

Indeed, I find the simple nature of it and the fact things tend not to
change is extremely useful. It is an advantage to have rules to abide
by that cannot be ignored or by-passed.

Mel

unread,
Jan 16, 2008, 10:52:08 AM1/16/08
to
J. Peng wrote:

> 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

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.

Message has been deleted
0 new messages