469,298 Members | 2,148 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,298 developers. It's quick & easy.

Why does python behave so? (removing list items)

Why does python create a reference here, not just copy the variable?
>>j=range(0,6)
k=j
del j[0]
j
[1, 2, 3, 4, 5]
>>k
[1, 2, 3, 4, 5]

Shouldn't k remain the same?

--
Micha³ Bentkowski
mr*****@gmail.com
Mar 26 '08 #1
4 1137
On Mar 26, 4:04 pm, "Micha³ Bentkowski" <mr.e...@gmail.comwrote:
Why does python create a reference here, not just copy the variable?
>j=range(0,6)
k=j
del j[0]
j
[1, 2, 3, 4, 5]
>k

[1, 2, 3, 4, 5]

Shouldn't k remain the same?
http://www.effbot.org/zone/python-list.htm
Mar 26 '08 #2
Micha³ Bentkowski pisze:
Why does python create a reference here, not just copy the variable?
Because Python works like that -- it uses names and values idiom. If you
change value, all names will be bound to the same changed value.
>>>j=range(0,6)
k=j
del j[0]
j
[1, 2, 3, 4, 5]
>>>k
[1, 2, 3, 4, 5]

Shouldn't k remain the same?
No further comments on this.

--
Jarek Zgoda
http://zgodowie.org/

"We read Knuth so you don't have to" - Tim Peters
Mar 26 '08 #3
Micha³ Bentkowski:
Why does python create a reference here, not just copy the variable?
I think to increase performance, in memory used and running time (and
to have a very uniform way of managing objects).

Bye,
bearophile
Mar 26 '08 #4
On Mar 26, 5:28*pm, bearophileH...@lycos.com wrote:
Micha³ Bentkowski:
Why does python create a reference here, not just copy the variable?

I think to increase performance, in memory used and running time (and
to have a very uniform way of managing objects).

Bye,
bearophile
A variable is a name-value pair. It's undefined outside of one-to-
ones therein. You have two names. Do they refer to the same object.
There is no such thing as the "primitive contents" of -any- standard
object.
Mar 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

699 posts views Thread by mike420 | last post: by
7 posts views Thread by Christian Neumann | last post: by
12 posts views Thread by Fred Pacquier | last post: by
10 posts views Thread by Andrew Dalke | last post: by
30 posts views Thread by Steven Bethard | last post: by
2 posts views Thread by Ryan | last post: by
5 posts views Thread by Karyn Williams | last post: by
3 posts views Thread by james.kirin40 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.