by w0utert · 12y ago · 0 comments
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.