Very often, I find myself coding trivial datatypes like
class Pruefer:
def __init__(self, ident, maxNum=float('inf'), name=""):
self.ident = ident
self.maxNum = maxNum
self.name = name
While this is very useful (Clearly I don't want to replace the above with anonymous 3-tuples), it's also very boilerplate.
Now for example, when I want to use the class in a dict, I have to add more boilerplate like
def __hash__(self):
return hash(self.ident, self.maxNum, self.name)
I admit that it might be difficult to recognize a general pattern amongst all my boilerplate classes, but nevertheless I'd like to as this question:
Are there any popular idioms in python to derive quick and dirty datatypes with named accessors?
Or maybe if there are not, maybe a Python guru might want to show off some metaclass hacking or class factory to make my life easier?
namedtuple
is good enough (added full answer with code example)namedtuple
now allows default values in 3.7+