473,761 Members | 10,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing argument value

>>def fooA(y):
y = [3,4]
return y
>>def fooB(y):
y[0] = 3
y[1] = 4
return y
>>x = [1,2]
fooA(x)
[3, 4]
>>x
[1, 2]
>>fooB(x)
[3, 4]
>>x
[3, 4]
===============

From above, the original argument value of fooA is same as before
[1,2]
but the original argument value of fooB is changed from [1,2] to
[3,4].

What is the difference between "y = [3,4]" and "y[0]=3

y[1] =4 "
Thanks,

SC
Dec 13 '07 #1
3 1511
flyfree a écrit :
>>>>def fooA(y):

y = [3,4]
return y

>>>>def fooB(y):

y[0] = 3
y[1] = 4
return y

>>>>x = [1,2]
fooA(x)

[3, 4]
>>>>x

[1, 2]

>>>>fooB(x)

[3, 4]
>>>>x

[3, 4]
===============

From above, the original argument value of fooA is same as before
[1,2]
but the original argument value of fooB is changed from [1,2] to
[3,4].

What is the difference between "y = [3,4]" and "y[0]=3
y[1] =4 "
In the first case, you rebind the local name y to a new list object -
and since the name is local, rebinding it only affects the local
namespace. In the second case, you mutate the list object (bound to the
local name y) passed to the function. The point to remember is that in
Python, all variables are references to objects, but names (including
params) are local.

HTH
Dec 13 '07 #2
On Thu, 13 Dec 2007 22:52:56 +0100, Bruno Desthuilliers wrote:
flyfree a écrit :
[snip]
>What is the difference between "y = [3,4]" and "y[0]=3 y[1] =4 "

In the first case, you rebind the local name y to a new list object -
and since the name is local, rebinding it only affects the local
namespace. In the second case, you mutate the list object (bound to the
local name y) passed to the function. The point to remember is that in
Python, all variables are references to objects, but names (including
params) are local.

HTH
Even though you have the assignment operator *in both cases*, it does
**not** issue the same thing.

As Bruno pointed out, in the first case ``y = [3,4]`` it is *rebinding*
the name `y`. When used with slice notation, the assignment operator
tells the object bound to y, "Hey, change your item with name/number 0 to
3, please." This can succeed but it doesn't have to. Raw assignments
always succeed.

(You could also make assignments of y[0] result in whatever you want, a
SystemExit for example; not so with pure assignments of y! The behaviour
of assignments cannot be influenced in any way.)

If you're interested in the special methods Python uses there, `Emulating
container types <http://docs.python.org/ref/sequence-
types.html#l2h-232>`_ might be interesting for you.

To sum up, the assignment operator has multiple meanings depending on its
left-hand-side operand.

HTH,
Dec 14 '07 #3
Stargaming <st********@gma il.comwrote:
>
Even though you have the assignment operator *in both cases*, it does
**not** issue the same thing.

As Bruno pointed out, in the first case ``y = [3,4]`` it is *rebinding*
the name `y`.
There have been two good replies to this, but I would like to present the
"mental model" I use to understand this. The term "rebinding" is not one
that folks encounter very often, and so doesn't bring up an image.

In Python, you have objects (which do not have names), and you have names.
Names get "bound" to objects. For example:

x = [1,2,3]

This statement creates an anonymous object out in space. The object is a
list, containing three numbers. It also creates a name "x" in a namespace,
and binds it to that anonymous list.

Now, let's say I do this:

x = [1,2,6]

This creates a NEW anonymous object out in space -- a list containing three
numbers -- and binds the name "x" to it. For a short time, we now have TWO
three-element lists in our object space. The old list ([1,2,3]) now has
nothing bound to it, so it will eventually be cleaned up by the garbage
collector. x is now bound to an entirely different object.

But, if I did this INSTEAD of that:

x[2] = 6

This does NOT create a new list. Instead, it changes one element in that
first anonymous list object we created. x is still bound to that same
list.

So, consider your example:

def fooA(y):
y = [3,4]
return y

def fooB(y):
y[0] = 3
y[1] = 4
return y

x = [1,2]
fooA(x)

"x" is just a name in the global namespace. We don't really pass "x" to
the function. Instead, we pass that anonymous list object with
two-elements. As the function is called, that list gets bound to the name
"y" inside fooA. This list now has TWO names bound to it, "x" in the
global namespace, and "y" in the fooA function.

When you execute
y = [3,4]
you are creating a brand-new list with two elements, and bunding that to
the local name "y". The old list STILL EXISTS, and since it is still bound
to the global "x", it won't be cleaned up. When the function returns, the
name "y" goes away, so it gets unbound. Since you don't store the function
result anywhere, the [3,4] list now has no names bound to it, and will get
cleaned up.

fooB(x)

Like before, this is passing the two-element [1,2] list into fooB, where it
gets bound to "y" inside fooB. Again, it has two names bound to it. Then,
when you do
y[0] = 3
you are changing an element in that list. Since that same list is bound to
both the global "x" and the local "y", the effect of the changes WILL be
seen when you exit from function.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 15 '07 #4

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

Similar topics

1
2331
by: z0ink | last post by:
I am working on a small graphing application. In the process of graphing I use 3 seperate scripts for getting the job done. The first is the page that the use sees and selects all the data from. This selected data gets passed internally using session variables to the next page. This page takes the passed data, runs it for error checking and determines where to send it next. I have gotten most of the desired results through this method...
8
3968
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
14
2511
by: Brandon Hoppe | last post by:
I'm trying to change the src of an ilayer in the parent document from a link inside the ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is for Netscape 4 only. For example, here is the main page: <html> <head> <script type="text/javascript">
2
1723
by: Urs Vogel | last post by:
Hi When using XmlDocument, I can create nodes and attributes as I like. What I didn't achieve is changing the Value of a node (created with createElement), it claims that it's the wrong node type. When changing the InnerText, I loose all child nodes. When cloning the node and reassigning the child nodes of the original node to the cloned node, the children are not visilble in the InnerXml. To make it simple. How do I change the value...
5
6060
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user hits the right and left keys to move this insertion point (cursor) Here is the problem:
64
3397
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be "refactor-ed" to be
4
2384
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw exceptions?
2
3205
by: paul | last post by:
I have a JS function to change the width of a <divthat works great in Firefox, but not at all in IE7. In IE an error message occurs: Line: 92 Char: 5 Error: Invalid Argument Code: 0 Firefox reports no errors in the Error Console, or Firebug, and the <divis resized correctly. Here is the function:
10
6469
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class MyObject<T: ChangeObject { ... public MyObject(string name, T value)
0
9531
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6609
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3881
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

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.