Last active
August 29, 2015 14:26
-
-
Save fdemmer/0c93749c515d9a5f3d63 to your computer and use it in GitHub Desktop.
a named tuple factory function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def namedtuple_factory(name=None, **kwargs): | |
"""create and use namedtuples in one line""" | |
name = name or 'AnonymousNamedtuple' | |
cls = namedtuple(name, kwargs.keys()) | |
return cls(**kwargs) | |
# sample usage | |
namedtuple_factory('Response', errorcode=500) | |
namedtuple_factory(id=1, name='anon') | |
namedtuple_factory(**{'this_dict': 1, 'is_a': 2, 'named': 'tuple'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment