I’m thinking about a situation where I have an object “Transaction”, that has quite a few properties to it like account, amount, date, currency, type, etc.
I never plan to mutate these data points, and calculation logic will live in other classes. My question is, is it poor Python design to instantiate thousands of objects just to hold data? I find the data far easier to work with embedded in a class rather than trying to cram it into some combination of data structures.
No, this is perfectly fine. In fact, Python has support for it in the standard
collectionsmodule:instead of
class Transaction(object):etc. Note thatnamedtupleis a kind of “class factory” and you need to pass it the name of the class to be constructed, as a string. That need not be the name you bind the result to, but doing so is still a good idea. Named tuple types are analogous to records in Pascal or structs in C: data holders with named members but no significant behavior of their own.Usage: