473,397 Members | 1,950 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,397 software developers and data experts.

[noob question] References and copying

Hello!

Where can I find a good explanation when does an interpreter copy the
value, and when does it create the reference. I thought I understand
it, but I have just typed in following commands:
a=[[1,2],[3,4]]
b=a[1]
b=[5,6]
a [[1, 2], [3, 4]] b

[5, 6]

And I don't understand it. I thought, that b will be a reference to a,
so changing b should change a as well. What do I do wrong. And a
second question - can I create a reference to element of a list of
floating points and use this reference to change that element?

Greets to all PyFans
zefciu
Jun 9 '06 #1
8 1306
zefciu wrote:
Hello!

Where can I find a good explanation when does an interpreter copy the
value, and when does it create the reference. I thought I understand
it, but I have just typed in following commands:
a=[[1,2],[3,4]]
b=a[1]
b=[5,6]
a [[1, 2], [3, 4]] b
[5, 6]

And I don't understand it. I thought, that b will be a reference to a,
so changing b should change a as well.


It would if you had written "b[:]=[5,6]"
What do I do wrong. And a
second question - can I create a reference to element of a list of
floating points and use this reference to change that element?
not like that.

Greets to all PyFans
zefciu

Jun 9 '06 #2
> Where can I find a good explanation when does an interpreter copy the
value, and when does it create the reference. Any good Python book. I have Learning Python and Programming Python 2nd
edition and they are very good IMO.
I thought I understand
it, but I have just typed in following commands:
a=[[1,2],[3,4]]
b=a[1]
b=[5,6]
a [[1, 2], [3, 4]] b

[5, 6]

And I don't understand it. I thought, that b will be a reference to a,
so changing b should change a as well.


No, you've set the name b to reference a slice of a. Slicing a list
always returns a new list.
To change a via b, do this:

a = [[1,2],[3,4]]
b = a

print a
print b

b[1] = [5,6]

print a
print b

Jun 9 '06 #3
zefciu wrote:
Hello!

Where can I find a good explanation when does an interpreter copy the
value, and when does it create the reference. I thought I understand
it, but I have just typed in following commands:

a=[[1,2],[3,4]]
b=a[1]
b=[5,6]
a
[[1, 2], [3, 4]]
b


[5, 6]

And I don't understand it. I thought, that b will be a reference to a,
so changing b should change a as well. What do I do wrong. And a
second question - can I create a reference to element of a list of
floating points and use this reference to change that element?

Greets to all PyFans
zefciu


Nope, b is a reference to the same object referenced by a[1], but only
until you rebind it. Think of assignment (binding) as storing a pointer
to an object in a name.

So

a = [[2,3],[3,4]]

stores a pointer to a list in "a". The list itself holds two pointers to
(otherwise anonymous) lists. Then

b = a[1]

make b point to the same object as a[1] does.

At this point you could, for example, execute

b[0] = 42

Then when you printed the value of "a" you would see

[[1, 2], [42, 4]]

and you would see

[42, 4]

as the value of b. But you don't do that, you next do

b = [5, 6]

This stores a reference to an entirely different new list in "b", with
the results you observe.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 9 '06 #4
zefciu wrote:
Hello!

Where can I find a good explanation when does an interpreter copy the
value, and when does it create the reference.
Unless you explicitely ask for a copy (either using the copy module or a
specific function or method), you'll get a reference.
I thought I understand
it, but I have just typed in following commands:

a=[[1,2],[3,4]]
- creates a list object containing 2 list objects, the first one
containing 2 integer objects with respective values 1 and 2, the second
one containing 2 integer objects with respective values 3 and 4

- associates ('binds') the name 'a' with the list object. Now in the
current namespace, 'a' refers to this list.
b=a[1]
- binds the name 'b' with the second element of [the list bound to] 'a'
b=[5,6]
- *re*binds 'b' to a new list containing two integer objects with
respective values 5 and 6.
a
[[1, 2], [3, 4]]
b
[5, 6]

Which is exactly what you asked for (while not being what you thought
you asked for...).
And I don't understand it. I thought, that b will be a reference to a,
It was - before you rebind it to another object.
so changing b should change a as well.
To be pedantic, you don't change 'b'. You can either modify the object
bound to 'b' (which you did not) or rebind 'b' to another object (which
you did).
What do I do wrong.
confusing rebinding a name and modifying an object.

Try this to better see what happens
NB :
- id() returns the unique identifier of an object - actually, in
CPython, it's memory address,
- 'is' test for identity ( a is b <=> id(a) == id(b)
a = [[1, 2], [3, 4]]
id(a) 46912496884192 id(a[1]) 46912496914656 b = a[1]
id(b) 46912496914656 b is a[1] True b = [5, 6]
id(b) 46912496915520 b is a[1] False
Now to modify a[1] thru b : b = a[1]
id(b) 46912496914656 b is a[1] True # add an item
b.append(5)
b [3, 4, 5] b is a[1] True a[1] [3, 4, 5] # remove the first item
del b[0]
a[1] [4, 5] # replace actual content with something else
b[:] = [5, 6]
b [5, 6] b is a[1] True a [[1, 2], [5, 6]]


And a
second question - can I create a reference to element of a list of
floating points and use this reference to change that element?
Not directly - but this has nothing to do with a reference-or-value
problem. It's just that floats (like ints, strings and tuples) are
immutable. You'll need either to work with indices or to wrap your
floats in mutable objects. I'd recommand the first solution.
Greets to all PyFans
zefciu

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 9 '06 #5
da********@yahoo.com wrote:
Where can I find a good explanation when does an interpreter copy the
value, and when does it create the reference.


Any good Python book. I have Learning Python and Programming Python 2nd
edition and they are very good IMO.

I thought I understand
it, but I have just typed in following commands:

>a=[[1,2],[3,4]]
>b=a[1]
>b=[5,6]
>a


[[1, 2], [3, 4]]
>b


[5, 6]

And I don't understand it. I thought, that b will be a reference to a,
so changing b should change a as well.

No, you've set the name b to reference a slice of a. Slicing a list
always returns a new list.


Please verify before asserting:
a = [[1, 2], [3, 4]]
b = a[1]
b is a[1] True id(b) 46912496915448 id(a[1]) 46912496915448


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 9 '06 #6
In article <44***********************@news.free.fr>,
bruno at modulix <on***@xiludom.gro> wrote:
da********@yahoo.com wrote:

....
And I don't understand it. I thought, that b will be a reference to a,
so changing b should change a as well.

No, you've set the name b to reference a slice of a. Slicing a list
always returns a new list.


Please verify before asserting:
a = [[1, 2], [3, 4]]
b = a[1]
b is a[1] True id(b) 46912496915448 id(a[1]) 46912496915448


You're right - he actually didn't set the name b to reference a
slice of a. But if he had - slicing a list does return a new list.
Indexing, as in the example, returns the item object. Or, binds a
reference to the left hand side identifier, whatever, but there is
no way to bind anything to the list location.

Donn Cave, do**@u.washington.edu
Jun 9 '06 #7
> Please verify before asserting:
a = [[1, 2], [3, 4]]
b = a[1]
b is a[1] True id(b) 46912496915448 id(a[1])

46912496915448


Right, I must have had slicing on the brain.

Jun 9 '06 #8
"zefciu" wrote in message news:e6**********@inews.gazeta.pl...
Where can I find a good explanation when does an interpreter copy the
value, and when does it create the reference. I thought I understand
it, but I have just typed in following commands:
>a=[[1,2],[3,4]]
b=a[1]
b=[5,6]
a
[[1, 2], [3, 4]]
>b
[5, 6]

And I don't understand it. I thought, that b will be a reference to a,
so changing b should change a as well. What do I do wrong.
The assignment always copy the reference, never the value.
After b=a[1] the b refers to the list object [3,4].
After b=[5,6] the earlier binding is forgotten, the new list with
values [5,6] is created and the b is bound to the new list.

But if you did
b[0] = 5
b[1] = 6

then you would get the expected result. The reason is that
b[0] is bound to 3 inside the big list refered by a, and
it is rebound to 5.

The suggested b[:] = [5, 6] is a shortcut for that (in fact,
it is slighly more magical and powerful).

And a second question - can I create a reference to element of a list of
floating points and use this reference to change that element?
No. Because the trivial types and also the string type are immutable
(i.e. constant). The list would contain references to the constants.
You cannot change the value of any constant. You have to replace
the reference.

Another possibility is to create your own class that will represent
one floating point value but will be mutable. In other words, the object
of your class will be a container (refered from the list) and its
internal state--the floating number--will be changed using the
method of the container.

pepr
Jul 4 '06 #9

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

Similar topics

10
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
8
by: Bruce Vander Werf | last post by:
Because Rectangle is a value type, I know I can do this: Rectangle rect1, rect2; rect1.Top = 34; rect2 = rect1; //copies members rect1.Top = 46; //does not affect rect2 But what about an...
13
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes....
32
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
14
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your...
14
by: streamkid | last post by:
i'm a learning newbie at c++... and i have the following question... reading some source code, i saw this: int function(const void * one, const void * two) { int var1, var2; var1 =...
8
by: sore eyes | last post by:
Hi I just downloaded the free Watcom compiler and am having a little trouble with File IO http://www.openwatcom.org/index.php/Download I downloaded the following example, commented out the...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
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
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...

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.