Well, these two approaches are closely related! When you do
stuff.a
you really get access to
stulff.__dict__['a']
Similarly, you can subclass dict to make __getattr__ the same as __getitem__ , and so stuff.a will also work for your dict subclass.
The object approach is often convenient and useful when you know that the keys in your mapping will be all simple strings that are valid Python identifiers. If you have more complex keys, you need a "real" mapping.
Of course, you should also use objects when you need more than simple matching. This "more" will usually be an extra state or extra calculations on the return values.
You should also consider how others will use your stuff objects. If they know this is just a dict , then they also know that they can call stuff.update(other_stuff) , etc. This is not so clear if you give them back the object. Basically: if you think that they need to manipulate the keys and values โโof your stuff , like a regular dict , then you should probably make it a dict .
As for the most โpythonicโ way to do this, I can only say that I saw that libraries use both approaches:
The BeautifulSoup library parses HTML and gives you very dynamic objects in which both attributes and access elements have special values.
They could instead return dict objects, but there is a lot of extra state associated with each object, and therefore it makes sense to use a real class.
Of course, there are also many libraries that simply return regular dict objects โ these are the bread and butter of many Python programs.
source share