[Python-ideas] defaultattrgetter

Serhiy Storchaka storchaka at gmail.com
Thu Dec 22 11:32:17 CET 2011

22.12.11 11:55, John O'Connor написав(ла):
> 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})

_xy = attrgetter('x', 'y', defaults=(True, False))

Sometimes, however, will be better to specify not value, but factory 
(when value is list, dictionary, or complex object), as for defauldict.

_xyz = defaultattrgetter('x', ('y', int, 123), ('z', list))
x has not default value,
y has default value int(123),
z has default value list().

More information about the Python-ideas mailing list