[Python-Dev] Re: "groupby" iterator

Christos Georgiou tzot at sil-tec.gr
Wed Dec 3 07:17:44 EST 2003


I don't know if this will be useful to anyone, but what the heck...

This post does not propose a complete substitute for lambda, but covers
most of its use cases using current python capabilities and syntax.
It's a very similar idea to what Michael Hudson already has mentioned.
And it doesn't apply to lambdas with more than one argument.

(There is also a module of mine, at
http://www.sil-tec.gr/~tzot/predicates.py that implements this idea in a
somehow harsh way to implement all and any predicates on iterables
--check at the end of the module for example usage.)

The whole idea is a C-implemented (after all, extract et al are about
speed) class, say Evaluator or Eval for short, which has an
implementation for all __*__ special methods; calling (directly or
indirectly) any special method of an instance of Eval returns a callable
that does exactly that operation to its argument.  Precedence etc are
taken care of by python.

So, this way,
>>> myf= lambda x: x**2 + 1
is equivalent to
>>> myf= Eval()**2 + 1

attrgetter("attribute_name") becomes Eval().attribute_name

and indexgetter(index) becomes Eval()[index]

However, I have no obvious way to replace stuff as

lambda x: x[:2] + x[-2:]

or

lambda x: divmod(x, 6)

although in my mentioned module above I have a special .do() method for
the latter case, but I presume most of you would find that ugly.

So, this idea covers the attrgetter and indexgetter use cases and then
some more, but it is not a complete lambda replacement.  Just my two
eurocents :-|
-- 
Christos Georgiou, Customer Support Engineer
Silicon Technologies S.A.
41-45 Kifissias Ave, Maroussi 151 23 Greece
Tel +30 (21) 06152600 (ext. 605) Fax +30 (21) 06198140
"Dave always exaggerated." --HAL



More information about the Python-Dev mailing list