473,394 Members | 1,740 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,394 software developers and data experts.

Why are tuples immutable?

jfj

Yo.

Why can't we __setitem__ for tuples?
The way I see it is that if we enable __setitem__ for tuples there
doesn't seem to be any performance penalty if the users don't use it
(aka, python performance independent of tuple mutability).

On the other hand, right now we have to use a list if we want to
__setitem__ on a sequence. If we could use tuples in the cases where
we want to modify items but not modify the length of the sequence,
programs could be considerably faster. Yes?

Enlighten me.

G.

Jul 18 '05 #1
3 5834
jfj wrote:

Yo.

Why can't we __setitem__ for tuples?
The way I see it is that if we enable __setitem__ for tuples there
doesn't seem to be any performance penalty if the users don't use it
(aka, python performance independent of tuple mutability).

On the other hand, right now we have to use a list if we want to
__setitem__ on a sequence. If we could use tuples in the cases where
we want to modify items but not modify the length of the sequence,
programs could be considerably faster. Yes?

Enlighten me.

G.

Well, the best answer may be "tuples are immutable because otherwise
there wouldn't be an immutable sequence type".

There have been many discussions on this topic (not that I'm blaming you
for being unaware of them), and normally when someone raises this topic
there's a flurry of nbew justifications for the existing behavior. The
bottom line seems to be that Guido wanted something that corresponds to
a mathematical tuple, which is an ordered collection of items of
(potentially) different types.

Of course there's nothing much to stop you using lists instead of
tuples, except the rare piece of code that insists on one or the other.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #2
Wasn't part of the point, that function call returns ought to be
immuntable. Otherwise you can accidentally end up modifying objects
that are referenced in other places ?

Obviously tuples aren't the *whole* answer... but they help.

Regards,

Fuzzy
http://www.voidspace.org.uk/atlantib...thonutils.html

Jul 18 '05 #3
jfj wrote:

Why can't we __setitem__ for tuples?


It seems from your suggestions here that what you really want is a
single sequence type, list, instead of two sequence types: tuple and
list. Under your design, list would support hash, and it would be up to
the programmer to make sure not to modify a list when using it as a key
to a dict. The important thing to understand is that, while this is not
an unreasonable design decision, it's not the design decision that's
been made for Python.

Reading the docs and some of GvR's comments on python-dev, my
understanding is that, while a single sequence type would have sufficed,
the belief was that there were two mostly non-overlapping sets of tasks
that one might want to do with sequences. One set of tasks dealt with
"small collections of related data which may be of different types which
are operated on as a group"[1] (tuples), while the other set of tasks
dealt with "hold[ing] a varying number of objects all of which have the
same type and which are operated on one-by-one"[1] (lists).

Now, when you use a sequence as a key to a dict, you're operating on the
sequence as a group, not one-by-one. Given the above conceptions of
tuple and list then, it is natural to provide tuple with hash support,
while leaving it out of list.

Note also that tuples being immutable also falls out of the tuple
description above too. If you're operating on the sequence as a group,
then there's no need for __setitem__; __setitem__ is used to operate on
a sequence one-by-one.

I understand that you'd like a type that is operated on one-by-one, but
can also be considered as a group (e.g. is hashable). Personally, I
don't have any use for such a type, but perhaps others do. The right
solution in this case is not to try to redefine tuple or list, but to
write a PEP[2] and suggest a new datatype that serves your purposes.
I'll help you out and provide you with an implementation: =)

class hashablelist(list):
def __hash__(self):
return hash(tuple(self))

Now all you need to do is write the Abstract, Motivation and Rationale,
and persuade the people on c.l.py and python-dev that this is actually a
useful addition to the language.

Steve

[1]
http://www.python.org/doc/faq/genera...ist-data-types
[2] http://www.python.org/peps/pep-0001.html
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ivan Voras | last post by:
Are there any performance/size differences between using tuples and using lists? -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke
42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
5
by: Gerrit Holl | last post by:
Hi, the FAQ says: > For example, a Cartesian coordinate is appropriately represented as a > tuple of two or three numbers. I find it strange to use tuples for a coordinate. After all, a...
3
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
15
by: Scott | last post by:
I'm going to start grouping all my questions in one post as this is my second today, and sorta makes me feel dumb to keep having to bother you all with trivial questions. I'll just seperate my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.