[Python-ideas] Dict-like object with property access (original) (raw)

Steven D'Aprano steve at pearwood.info
Tue Jan 31 01:28:53 CET 2012


Nick Coghlan wrote:

Outside the standard library, it's "practicality beats purity" that wins. People like being able to type ".attr" instead of "['attr']" when a field name happens to be a legal identifier.

Yes, but people like all sorts of things that aren't good for them, and while Python can and does allow people to shoot themselves in the foot, I don't think we should be providing a standard foot-shooting class :)

As I see it, x.name is best used for attributes of the object, which typically means that (1) in some sense, they are inherently part of the object; (2) they come from a relatively small enumerated set of names which is usually fixed; and (3) you are very unlikely to be added or deleting them from an object (usually a static set rather than dynamic).

x['name'] is for the opposite cases:

(1) the key/value pairs are not inherently part of the object, but in some sense are possessions of the object; (2) they come from a potentially arbitrarily large set of names which could grow indefinitely large; and (3) are usually dynamic rather than static.

In my experience, there are very few use-cases which fall between those two extremes and don't see the need for a standard Javascript-like dotted-access dict. But I think the first place to put one would be a recipe on (say) ActiveState's cookbook, and see what sort of feedback and use it gets.

-- Steven



More information about the Python-ideas mailing list