Skip to content

Instantly share code, notes, and snippets.

@ksindi
Created December 22, 2015 16:23
Show Gist options
  • Save ksindi/a224841ee169f8d5c1dc to your computer and use it in GitHub Desktop.
Save ksindi/a224841ee169f8d5c1dc to your computer and use it in GitHub Desktop.
access namedstuples as dictionary
# from http://pythondoeswhat.blogspot.com/2011/09/namedtupledict.html
def namedtupledict(*a, **kw):
namedtuple = collections.namedtuple(*a, **kw)
def getitem(self, key):
if type(key) == str:
return getattr(self, key)
return tuple.__getitem__(self, key)
namedtuple.__getitem__ = getitem
return namedtuple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment