Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created August 28, 2011 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/1176555 to your computer and use it in GitHub Desktop.
Save zeffii/1176555 to your computer and use it in GitHub Desktop.
alternative to namedtuple is auto_class
# as suggested and clarified by Campbell Barton, in the first response to this post.
# Here's an example of a helper function to do new classes that work like named tuples
# ----
# 1 liner for making new named tuple like classes
def auto_class(name, slots): return type(name, (object, ), {"__slots__": slots})
mytype = auto_class("TrickyClass", ("blah", "whee", "whoo"))
t = mytype()
t.whoo = 10
print(t.whoo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment