1

How do I define a constant list of namedtuples? I wish to define something like this in a module and use it throughout my code. I will then use filters on it when I'm only interested in ADULTS for example.

However, I dont want other coders to be able to corrupt this data, which they can do if I use a list. I realise that I could use a tuple but my understanding is that there is a de facto standard that tuples are used for heterogeneous data while I actually have homogeneous data:

http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/

If I have understood this article correctly.

human_map = [
   Activity(ADULT, EATING, OFTEN, 5),
   Activity(ADULT, SLEEPING, PERIODIC, 24),
   Activity(BABY, CRYING, OFTEN, 1)]
Baz
  • 12,713
  • 38
  • 145
  • 268
  • i dint understand what you mean *tuples are used for heterogeneous data while I actually have homogeneous data*. could you explain. – sundar nataraj Sep 17 '14 at 08:17
  • You can use a tuple. A tuple works much the same as a list but can't be altered. – khelwood Sep 17 '14 at 08:19
  • @khelwood I understand that I can use a tuple, but I was under the impression that it was standard practice to only have heterogeneous data in a tuple: http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/ – Baz Sep 17 '14 at 08:22
  • @Baz There's nothing to stop you using it for homogeneous data, and it is certainly often used that way, whatever a blog may say. – khelwood Sep 17 '14 at 08:28
  • @khelwood But this blog is referred to in the top answer for the question "Whats the difference between list and tuples" on Stack Overflow: http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples – Baz Sep 17 '14 at 08:30
  • There's not much context in the page you linked, but it seems to be saying that tuples are useful for the purpose of having indexes associated with particular meanings. That doesn't mean you can't *also* use them for homogeneous data if it suits your purpose. – khelwood Sep 17 '14 at 08:36

3 Answers3

1

Lists and tuples are both for arbitrary Python objects. You can use one or another: the only useful difference is that tuples are immutable.

There are only 3 types of immutable sequences in Python: strings, unicode and tuples (in Python 2), or strings, tuples and bytes (in Python 3).

So, if you need an immutable list of objects, use tuples.

Reference: https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy

Juan Cespedes
  • 1,299
  • 12
  • 27
  • But isnt it best practice to use tuples for heterogeneous data?: http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/ – Baz Sep 17 '14 at 08:28
1

No matter which method you use, there's very little to stop a developer from making your name human_map refer to an entirely different immutable container. As an example:

human_map = (some, immutable, elements,)

And then the malicious developer does something like the following:

import humans
humans.human_map = (something, else, entirely,)

At some point in Python, you just need to stop worrying and trust the clients of your module a little. If people want to shoot themselves in the foot, let them.

Brett Lempereur
  • 815
  • 5
  • 11
1

Lists and tuples may be used for heterogeneous data, but in Python there's nothing wrong with using them for homogeneous data.

Other languages provide basic collections (eg arrays in C/C++) that can only hold homogeneous data; more complicated collections must be used for heterogeneous data, and in those languages it is best practice to use the more efficient arrays for homogeneous data, rather than incurring the overheads of the fancier types of collection. But that logic does not apply to Python.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182