This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients brett.cannon, serhiy.storchaka, zach.ware
Date 2012-12-16.11:29:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1355657366.58.0.727671464335.issue16694@psf.upfronthosting.co.za>
In-reply-to
Content
Here is a functional (and more effective) equivalent of attrgetter:

def attrgetter(attr, *attrs):
    """
    Return a callable object that fetches the given attribute(s) from its operand.
    After f=attrgetter('name'), the call f(r) returns r.name.
    After g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).
    After h=attrgetter('name.first', 'name.last'), the call h(r) returns
    (r.name.first, r.name.last).
    """
    if not attrs:
        if not isinstance(attr, str):
            raise TypeError('attribute name must be a string')
        names = attr.split('.')
        def func(obj):
            for name in names:
                obj = getattr(obj, name)
            return obj
        return func
    else:
        getters = tuple(map(attrgetter, (attr,) + attrs))
        def func(obj):
            return tuple(getter(obj) for getter in getters)
        return func
History
Date User Action Args
2012-12-16 11:29:26serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, zach.ware
2012-12-16 11:29:26serhiy.storchakasetmessageid: <1355657366.58.0.727671464335.issue16694@psf.upfronthosting.co.za>
2012-12-16 11:29:26serhiy.storchakalinkissue16694 messages
2012-12-16 11:29:26serhiy.storchakacreate