[Python-ideas] defaultattrgetter
Peter Otten
__peter__ at web.de
Thu Dec 22 10:27:49 CET 2011
Stefan Behnel wrote:
> Masklinn, 22.12.2011 09:25:
>> On 2011-12-22, at 09:09 , Stefan Behnel wrote:
>>>
>>>> What if you need a default value for one but not the others, or
>>>> different values for all three? Does this behavior really make sense?
>>>
>>> What would be the use case? Would you want the lookup process to
>>> continue on the default argument if an intermediate attribute is not
>>> found? And if a subsequent lookup fails? Take the corresponding default
>>> value again and keep looking up on it? That feels rather unpredictable
>>> from a users' point of view.
>> A multiple-*args attrgetter fetches different arguments, for a "lookup
>> process" you need a dotted path (attrgetter('foo.bar.baz')).
>
> Ah, thanks, I remembered that incorrectly then.
>
> In that case, I vote -1 for adding the feature in the first place, because
> it would complicate an otherwise simple API too much and the current
> syntax proposals are too verbose and too hard to read.
I still like the addition, I think
>>> attrgetter("last_name", default="unknown")(None)
'unknown'
would be the most common use case by far and
>>> location = None
>>> attrgetter("x", "y", "z", default=0)(location)
(0, 0, 0)
>>> attrgetter("x.unit", default=None)(location)
None # don't know if there's no location.x or no location.x.unit
are useful and easy enough to grasp.
More information about the Python-ideas
mailing list