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

Miki Tebeka report at bugs.python.org
Tue Feb 3 07:47:22 CET 2009


Miki Tebeka <miki.tebeka at gmail.com> added the comment:

Hmmm, too much time has passed and my 1bit memory has overflowed since :)

IIRC it has to do with the fact that not all nodes in BeautifulSoup has a
"name" attribute. I wanted to count how may "tr" there were, so
    len(filter(lambda n: n == "tr", map(attrgetter("name"), soup))) 
was what I wanted to do, but I got AttributeError.

The natural way to me would be 
    len(filter(lambda n: n == "tr", map(attrgetter("name", ""), soup))) 
but instead I had to do
    len(filter(lambda n: n == "tr", map(lambda n: gettattr(n, "name", ""),
soup)))

Another thing I can think of is for usage in count/max ... when some of the
objects don't have the attribute you're looking for and it's by design
(bad one
maybe).

You'd like to be able to do:
    max(map(itemgetter("time", 0)), articles)

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


More information about the Python-bugs-list mailing list