Redesigning Python's named tuples
&gt; Either way (or both) would be implemented in C for speed. It would allow named tuples to be created without having to describe them up front, as is done now. But it would also remove one of the principles that guided the design of named tuples, as Tim Peters said:<p>&gt; &gt; How do you propose that the resulting object T know that T.x is 1. T.y is 0, and T.z doesn&#x27;t make sense? Declaring a namedtuple up front allows the _class_ to know that all of its instances map attribute &quot;x&quot; to index 0 and attribute &quot;y&quot; to index 1. The instances know nothing about that on their own, and consume no more memory than a plain tuple. If your `ntuple()` returns an object implementing its own mapping, it loses a primary advantage (0 memory overhead) of namedtuples.
Post-decree, Ethan Furman moved the discussion to python-ideas and suggested looking at his aenum module as a possible source for a new named tuple. But that implementation uses metaclasses, which could lead to problems when subclassing as Van Rossum pointed out.<p>&gt; Jim Jewett&#x27;s suggestion to make named tuples simply be a view into a dictionary ran aground on too many incompatibilities with the existing implementation. Python dictionaries are now ordered by default and are optimized for speed, so they might be a reasonable choice, Jewett said. As Greg Ewing and others noted, though, that would lose many of the attributes that are valued for named tuples, including low memory overhead, access by index, and being a subclass of tuple.<p>&gt; Rodolà revived his proposal for named tuples without a declaration, but there are a number of problems with that approach. One of the main stumbling blocks is the type of these on-the-fly named tuples—effectively each one created would have its own type even if it had the same names in the same order. That is wasteful of memory, as is having each instance know about the mapping from indexes to names; the current implementation puts that in the class, which can be reused. There might be ways to cache these on-the-fly named tuple types to avoid some of the wasted memory, however. Those problems and concern that it would be abused led Van Rossum to declare the &quot;bare&quot; syntax (e.g. (x=1, y=0)) proposal as dead.<p>From what I&#x27;ve read, v8&#x27;s implementation of objects in Javascript goes basically like this: when you call a constructor function and assign properties to your object, it makes up struct types and ties the object to that type or something.<p>Like this:<p><pre><code>    function Point2D(x, y) {
        this.x = x;
        this.y = y;
    }
    let p = new Point2D(1.0, 2.0)
</code></pre>
initially you&#x27;ll have an empty object, which will be an empty struct. &#x27;this.x = x&#x27; will change the type to the &#x27;X&#x27; struct, and &#x27;this.y = y&#x27; will change the type to the &#x27;XY&#x27; struct. If you do this again with another object, <i>they&#x27;ll share these underlying structs</i>.<p>Now this is perhaps easier with a JIT, and perhaps not. But it bears thinking about. Why not just make it so that (x: 1, y: 0) - which would be the best syntax IMO as it fills out the {set, dict; tuple, ???} square - creates an object that shares its class with every other namedtuple that has exactly the x and y properties in exactly that order?<p>It <i>really</i> frustrates me when I read &#x27;Those problems and concern that it would be abused led Van Rossum to declare the &quot;bare&quot; syntax (e.g. (x=1, y=0)) proposal as dead.&#x27; I mean come on, I know it&#x27;s a different environment in Python than in V8, but seriously this is a solved problem. Those problems? Those problems are a solved problem that a solution was already proposed for in the thread. Just do that.<p>&gt;He elaborated on the ordering problem by giving an example of a named tuple that stored the attributes of elementary particles (e.g. flavor, spin, charge) which do not have an automatic ordering. That argument seemed to resonate with several thread participants.<p>I don&#x27;t want to be too harsh, but this is nonsensical rubbish. Dictionaries preserve order in Python. This ship sailed a long time ago. Namedtuples also already preserve order. Tuples preserve order. Lists preserve order. Dictionaries preserve order.<p>What <i>doesn&#x27;t</i> preserve order? Like, I get that it&#x27;s not strictly defined that dictionaries preserve order, but they do, and people do rely on that, and so it&#x27;s never going to actually be changed.<p>&gt;This is exactly why I scream at relational databases. If you can&#x27;t tell the difference between a set and a list, and especially if you want to store a list in a set-based paradigm, you are going to have ALL SORTS of grief ...<p>Unrelated but I found this comment funny. This guy has heard of an index, right?