[Python-ideas] A namedtuple literal.

Franklin? Lee leewangzhong+python at gmail.com
Tue Apr 15 03:18:04 CEST 2014


I don't see much of a necessary memory issue with namedtuple literals. Upon
creation of an anonymous namedtuple, you can map the tuple of names to a
single type object.

Sample pseudo-code (*smirk*) implementation for what the interpreter could
do, given the key-value pairs:

    def ntuple(pairs: OrderedDict):
        names = tuple(pairs.keys())
        try:
            tuple_factory = existing_ntuples[names]
        except ValueError:
            tuple_factory = namedtuple(None, names)
                # `None` not actually legal for namedtuple's name param
            existing_ntuples[names] = tuple_factory

        return tuple_factory(*pairs.values())

A different (ideological?) problem: namedtuples look like they're basically
made to be both tuples AND simple lightweight immutable *classes*. The
namedtuple literal implemented this way would be effectively allowing
reusable anonymous classes. I don't see too much of a problem with this,
though, since we should usually not care if they're ducks or really
monkeys. Here are some sample "abuses", though:
    a = (x=4, y=5)
    b = type(a)(6, 7)
    c = (x='snark', y='sarcasm')
    type(c) == type(b) #True

Actually, now that I write it out, I like that the type of an anonymous
named tuple depends on the property names, and that two anonymous ntuples
with different keylists are different.

On Mon, Apr 14, 2014 at 7:25 PM, Anthony Towns <aj at erisian.com.au> wrote:

> On 15 April 2014 07:44, Franklin? Lee <leewangzhong+python at gmail.com>
> wrote:
> > As long as we're just fooling around with ideas, here's an alternative
> > proposal for namedtuple literal syntax[0].
>
> Isn't a namedtuple literal a bad idea? Isn't the point of a namedtuple
> (as distinct from a dict or OrderedDict), that each object only stores
> the data and the names are stored in the type, making having lots of
> namedtuples more memory efficient? ("Named tuple instances do not have
> per-instance dictionaries, so they are lightweight and require no more
> memory than regular tuples." --
> https://docs.python.org/2/library/collections.html#collections.namedtuple)
>
> If you already have a specific namedtuple type instantiated, literals are
> easy:
>
>   Point = namedtuple('Point', ['x', 'y'])
>   Point(1,2)  # positional
>   Point(y=2, x=1) # named
>
> > - People might expect `p = (x=1) to be a 1-ntuple. I don't want it to be,
> > and I don't like that it wouldn't be.
>
> Adding a trailing comma like with normal 1-tuples to get "p = (x=1,)"
> would seem like the obvious approach? a=(b=1) is already invalid
> syntax though.
>
> Cheers,
> aj
>
> --
> Anthony Towns <aj at erisian.com.au>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140414/4b6ca976/attachment.html>


More information about the Python-ideas mailing list