Python's theoretically missing core data type

March 8, 2009

In theory in Python, tuples are for heterogeneous data and lists are for homogeneous data. Except, well, tuples and lists have 'side effects': tuples are read-only and lists are not. Which means that if you take the restriction on lists seriously, you do not have a core data type for ordered writeable heterogeneous data (and maybe also one for read-only homogeneous data).

(While this is a theory that's probably more honored in the breach than in practice, I think that it does have some consequences, especially for determining what things go into the standard library.)

All of which gets us to the issue of whether one should hijack lists for the situation where you need ordered writeable heterogeneous data. On the one hand, if lists are for heterogeneous data and this is their most important attribute, then people who hijack them are doing it wrong; not only are they writing un-Pythonic code, but they are probably not going to get the support they'd like from the standard library.

On the other hand, I think that ordered writeable heterogeneous data is a common enough pattern that there should be a core type that supports it; there are a fair number of situations with ordered, modifiable fields. (If nothing else, I note that any serialization format imposes a field order on otherwise unordered structures.)

In theory you could add another core type to Python for this. In practice I think it would be the wrong answer; there would be little functional difference between lists and the new data type, so you'd be adding it merely for intellectual purity. This is unlikely to be attractive to either language designers or Python programmers.

All of which leads me to the opinion that the list purists should yield a bit and accept that for the overall good of Python, lists can be legitimately used for heterogeneous data as well as homogeneous data. Carrying this through to what gets added to the standard library would be nice, too.

(Yes, I would like a 'namedlist' class in the collections module, especially since I wrote one.)


Comments on this page:

From 82.130.13.123 at 2009-03-08 12:54:54:

What use case do you have for ordered heterognous data?

By cks at 2009-03-08 15:01:10:

I assume you mean writable ordered heterogeneous data. One specific case I have written code for is processing /etc/passwd on a Unix system, and so a general use case is file formats with lines that have multiple fields with different sorts of data in them.

Written on 08 March 2009.
« What past problems of mine the collections module solves
What list methods don't make sense for heterogeneous lists »

Page tools: View Source, View Normal.
Search:
Login: Password:

Last modified: Sun Mar 8 01:25:44 2009
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.