[Python-ideas] defaultattrgetter

Arnaud Delobelle arnodel at gmail.com
Thu Dec 22 11:21:46 CET 2011

On Dec 22, 2011 9:56 AM, "John O'Connor" <jxo6948 at rit.edu> wrote:
> > I think the idea is that you ask for an attribute, which (in the less
> > cases) happens to be a multi-step lookup, and if the attribute is not
> > you want it to return a default value *for the attribute you requested*,
> > i.e. not a different value for any of the intermediate attributes, only
> > specific value that corresponds to the last attribute in the lookup
> > In the 99.9% case, that will be something like None or a "keep going, I
> > don't care" value, not something that depends on the lookup path in any
> >
> > I don't think the 0.1% case where you want more than that is worth a
> > substantially more complicated API.
> >
> I could settle for the default= but I think it is too simple and thus
> incomplete. I think it is extreme to say that one default argument
> covers 99.9% use case. I do think you are right about the ignored path
> but that path may use more than one condition.
> _xy = defaultattrgetter(('x', True), ('y', False))
> x, y = _xy(foo)
> # usually do x unless told otherwise
> if x: ...
> # usually dont do y
> if y: ...
> I guess one alternative could be:
> _xy = attrgetter('x', 'y', defaults={'x': True, 'y': False})
> but that just looks like DRY without the D.

You could have attrgetter('x', 'y', defaults=(True, False)) to put the D
back in.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111222/43e86ebb/attachment.html>

More information about the Python-ideas mailing list