[Python-Dev] Re: "groupby" iterator
David Eppstein
eppstein at ics.uci.edu
Wed Dec 3 13:50:03 EST 2003
In article
<200312031748.hB3Hm5504233 at c-24-5-183-134.client.comcast.net>,
Guido van Rossum <guido at python.org> wrote:
> > A minor correction: the Voodoo approach handles fine method calls by
> > defining a __call__ method in the Voodoo class; [...]
> >
> > lambda x: x.startswith("text")
> > This works fine as:
> > Voodoo().startswith("text")
>
> Hm. But the whole point of Voodoo is that the resulting object has a
> __call__ method that evaluates the expression for a given argument.
> How do you do that? (I suppose you can do it if you don't do
> chaining, but I like the chaining.)
I think you would need two kinds of voodoo.
extract.attr
could expand to
lambda x: x.attr
(or something similar via __call__ methods) but
method.startswith
could expand to
lambda *args: lambda x: x.startswith(*args)
Then you could do
sort(stringlist, key=method.count("kibo"))
or whatever.
Obviously this doesn't allow you to easily express e.g. methods of
attributes, but beyond a certain level of complexity you should be using
separate defs anyway.
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
More information about the Python-Dev
mailing list