On Mon, Jul 17, 2017 at 6:27 PM, Greg Ewing <[email protected]>
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
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com