[Python-Dev] Re: "groupby" iterator

Michael Hudson mwh at python.net
Tue Dec 2 14:23:35 EST 2003

Guido van Rossum <guido at python.org> writes:

> So again, here we have a mechanism that's rather generic (lambda)
> which is frequently used in a few stylized patterns (to extract an
> attribute or field).  So Raymond's new functions attrgetter and
> itemgetter (whose names I cannot seem to remember :-) take care of
> these.
> But, at least for attrgetter, I am slightly unhappy with the outcome,
> because the attribute name is now expressed as a string literal rather
> than using attribute notation.  This makes it harder to write
> automated tools that check or optimize code.  (For itemgetter it
> doesn't really matter, since the index is a literal either way.)
> So, while I'm not particularly keen on lambda, I'm not that keen on
> attrgetter either.  But what could be better?  All I can think of are
> slightly shorter but even more crippled forms of lambda; for example,
> we could invent a new keyword XXX so that the expression (XXX.foo) is
> equivalent to (lambda self: self.foo).  This isn't very attractive.

Doesn't have to be a keyword... I implemented something like this
years ago and then ditched it when list comps appeared.

It would let you do things like

>>> map(X + 1, range(2))
[1, 2, 3]

too, IIRC.


  > With Python you can start a thread, but you can't stop it.  Sorry.
  > You'll have to wait until reaches the end of execution.
  So, just the same as c.l.py, then?
                       -- Cliff Wells & Steve Holden, comp.lang.python

More information about the Python-Dev mailing list