Using Python class as a data container

class, dictionary, python, struct

Solution

If you're really never defining any class methods, a dict or a namedtuple make far more sense, in my opinion. Simple+builtin is good! To each his own, though.

Problem

Sometimes it makes sense to cluster related data together. I tend to do so with a dict, e.g., ``` group = dict(a=1, b=2, c=3) print(group['a']) ``` One of my colleagues prefers to create a class ``` class groupClass: def __init__(self, a, b, c): self.a = a self.b = b self.c = c group = groupClass(1, 2, 3) print(group.a) ``` Note that we are not defining any class methods. I like to use a dict because I like to minimize the number of lines of code. My colleague thinks the code is more readable if you use a class, and it makes it easier to add methods to the class in the future. Which do you prefer and why?

Original source

Related problems