fork download
  1. import collections
  2.  
  3.  
  4. def MetaNode(values):
  5.  
  6. class Meta(type):
  7.  
  8. def __new__(cls, name, bases, extra):
  9.  
  10. class AbstractNode(collections.namedtuple(name, values)):
  11.  
  12. def __new__(cls, *args, children=None, **kwargs):
  13.  
  14. return super().__new__(cls, *args, **kwargs)
  15.  
  16. def __init__(self, *args, children=None, **kwargs):
  17.  
  18. super().__init__()
  19. self.children = children or []
  20.  
  21. return super().__new__(cls, name, bases + (AbstractNode,), extra)
  22.  
  23. return Meta
  24.  
  25.  
  26. class RootNode(metaclass=MetaNode("a b c")):
  27.  
  28. pass
  29.  
  30.  
  31. root = RootNode(1, 2, c=3)
  32. print(root, root.children)
  33.  
  34. root = RootNode(1, 2, c=3, children=["yoba"])
  35. print(root, root.children)
Success #stdin #stdout 0.02s 10240KB
stdin
Standard input is empty
stdout
RootNode(a=1, b=2, c=3) []
RootNode(a=1, b=2, c=3) ['yoba']