(original) (raw)
On Mon, Jul 17, 2017 at 6:27 PM, Greg Ewing <greg.ewing@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)