[Python-ideas] Dict-like object with property access
Ethan Furman
ethan at stoneleaf.us
Mon Jan 30 21:11:44 CET 2012
Arnaud Delobelle wrote:
> On 30 January 2012 19:25, Ethan Furman <ethan at stoneleaf.us> wrote:
>>> I do. The object has two different interfaces for the same values and
>>> that's IMO unpythonic. The Zen says
>>>
>>> "There should be one-- and preferably only one --obvious way to do it."
>>
>> Indeed.
>>
>> When you have the attribute name stored in a variable, then the one obvious
>> way is [] access.
>
> If it's an attribute, the obvious way is getattr(obj, attrname)
Fair point.
I guess I'll be happy that Python will let me do it the way that works
better for me. :)
I think the thing to keep in mind is that this Bunch type object is not
general purpose, like a dict is: it is specialized -- only string keys,
only keys that don't conflict with methods (or no methods, depending on
the needs), etc., etc.
My custom object that works this way has attributes/methods divided by
size: 10 or less and it's an attribute; more and it's a method. Works
well for me. (10 because that's the maximum size for a field name in a
dbf table (version 5 and below, anyway).)
~Ethan~
More information about the Python-ideas
mailing list