On Mon, Jul 17, 2017 at 6:27 PM, Greg Ewing <greg.ew...@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)
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to