[noob question] References and copying

Z

zefciu

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
 
B

Boris Borcic

zefciu said:
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.
 
D

danmcleran

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
 
S

Steve Holden

zefciu said:
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]]

[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
 
B

bruno at modulix

zefciu said:
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:


- 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.

- binds the name 'b' with the second element of [the list bound to] 'a'

- *re*binds 'b' to a new list containing two integer objects with
respective values 5 and 6.
[[1, 2], [3, 4]]

[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 said:
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.
 
B

bruno at modulix

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]]

[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
 
D

Donn Cave

(e-mail address removed) wrote: ....
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, (e-mail address removed)
 
P

Petr Prikryl

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top