Connecting Tech Pros Worldwide Help | Site Map

Why does python behave so? (removing list items)

=?ISO-8859-2?Q?Micha=B3_Bentkowski?=
Guest
 
Posts: n/a
#1: Mar 26 '08
Why does python create a reference here, not just copy the variable?
Quote:
Quote:
Quote:
>>j=range(0,6)
>>k=j
>>del j[0]
>>j
[1, 2, 3, 4, 5]
Quote:
Quote:
Quote:
>>k
[1, 2, 3, 4, 5]

Shouldn't k remain the same?

--
Micha³ Bentkowski
mr.ecik@gmail.com
Waldemar Osuch
Guest
 
Posts: n/a
#2: Mar 26 '08

re: Why does python behave so? (removing list items)


On Mar 26, 4:04 pm, "Micha³ Bentkowski" <mr.e...@gmail.comwrote:
Quote:
Why does python create a reference here, not just copy the variable?
>
Quote:
Quote:
>j=range(0,6)
>k=j
>del j[0]
>j
[1, 2, 3, 4, 5]
Quote:
Quote:
>k
>
[1, 2, 3, 4, 5]
>
Shouldn't k remain the same?
http://www.effbot.org/zone/python-list.htm
Jarek Zgoda
Guest
 
Posts: n/a
#3: Mar 26 '08

re: Why does python behave so? (removing list items)


Micha³ Bentkowski pisze:
Quote:
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.
Quote:
Quote:
Quote:
>>>j=range(0,6)
>>>k=j
>>>del j[0]
>>>j
[1, 2, 3, 4, 5]
Quote:
Quote:
>>>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
bearophileHUGS@lycos.com
Guest
 
Posts: n/a
#4: Mar 26 '08

re: Why does python behave so? (removing list items)


Micha³ Bentkowski:
Quote:
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
castironpi@gmail.com
Guest
 
Posts: n/a
#5: Mar 27 '08

re: Why does python behave so? (removing list items)


On Mar 26, 5:28*pm, bearophileH...@lycos.com wrote:
Quote:
Micha³ Bentkowski:
>
Quote:
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.
Closed Thread