473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

no pass-values calling?

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!
Jan 16 '08 #1
15 1357
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-object.htm

for further details.
--
Steven
Jan 16 '08 #2
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

Jan 16 '08 #3
On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <pe******@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!
Jan 16 '08 #4
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
Jan 16 '08 #5
On Jan 16, 7:59 am, "J. Peng" <peng....@gmail.comwrote:
On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
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.
Jan 16 '08 #6
Christian Heimes <li***@cheimes.dewrites:
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
Jan 16 '08 #7
On Jan 16, 2008 2:30 PM, Steven D'Aprano
<st****@remove.this.cybersource.com.auwrote:
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
Jan 16 '08 #8
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

Jan 16 '08 #9
On Jan 16, 2008 3:03 PM, Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Wed, 16 Jan 2008 13:59:03 +0800, "J. Peng" <pe******@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?
Jan 16 '08 #10
"J. Peng" <pe******@gmail.comwrites:
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
Jan 16 '08 #11
Christian Heimes <li***@cheimes.dewrites:
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
Jan 16 '08 #12
On Wed, 16 Jan 2008 17:46:39 +1100, Ben Finney wrote:
Christian Heimes <li***@cheimes.dewrites:
>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
Jan 16 '08 #13
J. Peng a écrit :
On Jan 16, 2008 2:30 PM, Steven D'Aprano
<st****@remove.this.cybersource.com.auwrote:
>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.

Jan 16 '08 #14
Hallöchen!

J. Peng writes:
On Jan 16, 2008 2:30 PM, Steven D'Aprano
<st****@remove.this.cybersource.com.auwrote:
>On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:
>>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.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for further contact info.)
Jan 16 '08 #15
Mel
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.
Jan 16 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Zlatko Matiæ | last post by:
Hello. I'm wondernig what is happennig whith saved pass-through queries nested in regular JET query if regular JET query just filtrates result by start/end date...Does pass-through query first...
0
by: Zlatko Matiæ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
1
by: Greg Strong | last post by:
Hello All, Why would brackets be added to the SQL of a pass through query to Oracle? If I paste the debug print of the SQL statement into SQLPlus of Oracle's XE edition it works, and does NOT...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
15
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
5
by: marshmallowww | last post by:
I have an Access 2000 mde application which uses ADO and pass through queries to communicate with SQL Server 7, 2000 or 2005. Some of my customers, especially those with SQL Server 2005, have had...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.