[issue4124] Patch for adding "default" to itemgetter and attrgetter

Raymond Hettinger report at bugs.python.org
Fri Jan 30 23:50:37 CET 2009


Raymond Hettinger <rhettinger at users.sourceforge.net> added the comment:

Am curious about your use cases.  ISTM that in every case I've every
used either function, I've always known that the attribute or item is
going to be there.  For instance, the original motivating use cases were
to support the key= argument to
min/max/sorted/nlargest/nsmallest/groupby and to support iterator
algebra with itertools:

def powerset(iterable):
    '''Iterate over all subsets.

    >>> list(powerset('abc'))
    [set([]), set(['a']), set(['b']), set(['a', 'b'])]

    '''
    # Only 1 initial call to PyObject_Hash().  
    # No trips around the eval-loop.
    # Memory friendly.
    seq = map(set, list(iterable)[::-1])
    selector_stream = product([False, True], repeat=len(seq))
    newsets, ns1 = tee(starmap(set, repeat(())))
    components = imap(compress, repeat(seq), selector_stream)
    sets_and_components = imap(chain, izip(newsets), components)
    results = starmap(set.update, sets_and_components)
    return imap(itemgetter(0), izip(ns1, results))

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4124>
_______________________________________


More information about the Python-bugs-list mailing list