473,471 Members | 4,648 Online
Bytes | Software Development & Data Engineering Community
Create 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 1494
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********@gmail.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
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. ...
8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
14
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....
2
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...
5
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...
64
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...
4
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...
2
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...
10
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...
0
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,...
0
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,...
0
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...
1
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.