point = (0, 0) point = (0, 0)Of course there are many cases where using tuples is the best way to represent some data item, (x, y) point data is an obvious example. But when your data structures become more complex, need to be mutable, need to have operations defined on them, have relations to other objects, etc, tuples are a bad choice. You don't want to end up with code that uses tuples of varying lengths, composed of elements of varying types, consumed and produced by functions that are hard-coded to rely on the exact length, ordering and element type of their their tuple inputs. Named tuples are one step in the direction of dictionaries, but they are still nowhere as flexible as mutable dictionaries or proper classes.
Just as a thought exercise, try to come up with a way to represent and manipulate an hierarchical, mutable C-structure with 20 fields, using nothing but tuples in Python.
Of course, you're not restricted to using one datatype in C either, but the constant comparison this article makes between structs and dictionaries is misleading. Comparing hash map implementations with dictionaries would be much more apt.
http://dev.svetlyak.ru/using-slots-for-optimisation-in-pytho...
http://hg.python.org/releasing/2.7.6/file/ba31940588b6/Lib/c...
I am wrong. namedtuple does NOT using slots. weird, wonder why?
Because it directly subclasses `tuple`
point = namedtuple("Point","x y z",verbose=True)
http://blip.tv/pycon-us-videos-2009-2010-2011/pycon-2011-fun...
Yes if this was "production code" but for the sake of comparison in this example it should be a Python class vs. C struct.
And as the text describes, a dict behaves similarly to a class in Python when it comes to runtime perf.
His other argument, about summing points, makes no sense either. He doesn't seem to have even profiled his "slow" point summing code or tried basic Python optimisations like using a list comprehension or generator to sum the points. And if that doesn't work well enough, and point summing is a critical part of your program, then you'd use a library like NumPy to handle that, rather than using raw python.
Or a namedtuple, if you wanted to have access to the x and y fields as named attributes.
The article actually does mention this, but only in a footnote, and the footnote gets the facts wrong:
"One would be right to argue that collections.namedtuple() is even closer to C struct than this. Let’s not overcomplicate things, however. The point I’m trying to make is valid regardless."
No, it isn't, because a namedtuple does not work the same under the hood as the straightforward class definition he gives. If p = Point(0, 0) is a namedtuple instance, and you do p.x or p.y, what actually happens is that the "attribute" is looked up by index, which is equivalent to a C struct field access.
Has that changed?
Yes, you're right, I was mistaken about how namedtuples work under the hood.
If you have a class in Python, the member variable lookup will essentially be a hash table lookup (requires at least a little pointer traversal). By contrast, looking up a member variable is a compile time constant offset to a known pointer value. It's O(1) vs. O(1) but a huge difference in the constant factor.
Yep, using a tuple would be a little better if you were actually writing Python code but it would be comparing apples to oranges when it comes to this example case.
edit note: a dict and class are equal only in CPython, see this video linked in the OP http://vimeo.com/61044810 - it discusses various optimizations regarding classes vs. dicts in PyPy etc