Advertisement
Guest User

Untitled

a guest
Jul 12th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. from collections import namedtuple
  2.  
  3. def LenientNamedTuple(name, fields):
  4.     class Wrapper(namedtuple(name, fields)):
  5.         __slots__ = ()
  6.         def __new__(cls, *args, **kwargs):
  7.             newkw = {k: v for k, v in kwargs.items() if k in fields}
  8.             kwargs.clear()
  9.             kwargs.update(newkw)
  10.             return super(Wrapper, cls).__new__(cls, *args, **kwargs)
  11.     return Wrapper
  12.  
  13. class Foo(LenientNamedTuple('Foo', ['id', 'name', 'age'])):
  14.     def __init__(self, *args, **kwargs):
  15.         super(Foo, self).__init__()#*args, **kwargs)
  16.  
  17. print(Foo(1, 2, 3))
  18. print(Foo(id=1, name=2, age=3))
  19. print(Foo(1, age=3, name=2))
  20. print(Foo(id=1, name=2, age=3, spam=4))
  21. print(Foo(1, 2, 3, spam=4))
  22. print(Foo(1, 2, spam=4, age=3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement