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 8 1175
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
> 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
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
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('@')])" 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('@')])"
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
> 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.
"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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
10 posts
views
Thread by Matt Hollingsworth |
last post: by
|
1 post
views
Thread by Tony Johansson |
last post: by
|
8 posts
views
Thread by Bruce Vander Werf |
last post: by
|
13 posts
views
Thread by cgough |
last post: by
|
32 posts
views
Thread by Axel Bock |
last post: by
|
14 posts
views
Thread by Vols |
last post: by
|
14 posts
views
Thread by streamkid |
last post: by
|
8 posts
views
Thread by sore eyes |
last post: by
|
20 posts
views
Thread by tshad |
last post: by
| | | | | | | | | | |