473,578 Members | 3,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1371
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.net com.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.net com.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.cybersourc e.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.net com.comwrote:
On Wed, 16 Jan 2008 13:59:03 +0800, "J. Peng" <pe******@gmail .com>
declaimed the following in comp.lang.pytho n:

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

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

Similar topics

110
9857
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 must be an object instead of
3
2737
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 returns all rows from server and then JET filtrates result by start/end date, or JET first communicates with server so that server returns only those...
0
3311
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 appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding...
5
7808
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 a WebService is that essentially we are passing an address of an object which resides on the 'local machine' i.e. a local machine object address....
1
4280
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 have brackets around the tablespace preceding the table name. However when I create the pass through query in VB brackets are placed around the...
31
2888
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 statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of...
6
2701
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 pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
15
2008
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
6312
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 pass-through queries fail due to intermittent connection failures. I can easily restablish a connection for ADO. My problem is with...
12
11046
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed!...
0
7847
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8290
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7874
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6522
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5342
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.