Skip to content

Instantly share code, notes, and snippets.

@fdemmer
Last active August 29, 2015 14:26
Show Gist options
  • Save fdemmer/0c93749c515d9a5f3d63 to your computer and use it in GitHub Desktop.
Save fdemmer/0c93749c515d9a5f3d63 to your computer and use it in GitHub Desktop.
a named tuple factory function
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