By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Why does python behave so? (removing list items)

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.