[Python-Dev] Impact of Namedtuple on startup time

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Jul 17 19:48:05 EDT 2017


On Mon, Jul 17, 2017 at 6:27 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>
wrote:
>
>
> Maybe a metaclass could be used to make something
> like this possible:
>
>
>    class Foo(NamedTuple, fields = 'x,y,z'):
>       ...
>
>
If you think of it, collection.namedtuple *is* a metaclass.  A simple
wrapper will make it usable as such:

import collections

def namedtuple(name, bases, attrs, fields=()):
        # Override __init_subclass__ for Python 3.6
        return collections.namedtuple(name, fields)

class Foo(metaclass=namedtuple, fields='x,y'):
    pass

print(Foo(1, 2))   # ---> Foo(x=1, y=2)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170717/a08bf4e1/attachment.html>


More information about the Python-Dev mailing list