Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple
- def LenientNamedTuple(name, fields):
- class Wrapper(namedtuple(name, fields)):
- __slots__ = ()
- def __new__(cls, *args, **kwargs):
- kwargs = dict((k, v) for k, v in kwargs.items() if k in fields)
- return super(Wrapper, cls).__new__(cls, *args, **kwargs)
- return Wrapper
- class Foo(LenientNamedTuple('Foo', ['id', 'name', 'age'])):
- def __init__(self, *args, **kwargs):
- super(Foo, self).__init__()#*args, **kwargs)
- print(Foo(1, 2, 3))
- print(Foo(id=1, name=2, age=3))
- print(Foo(1, age=3, name=2))
- print(Foo(id=1, name=2, age=3, spam=4))
- print(Foo(1, 2, 3, spam=4))
- print(Foo(1, 2, spam=4, age=3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement