473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1289
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: Christian Neumann | last post by:
Hello, i have a problem with the built-in function xrange(). Could you by any chance be able to help? I use Python 2.3.4 (final) and i think there is a bug in the built-in
12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
2
by: Ryan | last post by:
I have a dropdown list bound to a datasource. I would like to clear it at the press of a button. I tried using the following code: mydropdown.Items.Clear() The end result is that it comes...
5
by: Karyn Williams | last post by:
I am new to Pyton. I am trying to modify and understand a script someone else wrote. I am trying to make sense of the following code snippet. I know line 7 would be best coded with regex. I first...
3
by: james.kirin40 | last post by:
Hi everyone, I am using Python's re module to extract some data from html. The following code never returns, and I was wondering if someone can explain to me why. Is this a problem with my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.