[Python-Dev] Re: "groupby" iterator

Christos Georgiou tzot at sil-tec.gr
Wed Dec 3 13:28:47 EST 2003

Guido van Rossum said:

>> 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.)

(replying to Phillip Eby too)

Yes, you are correct (I was thinking in terms of my All and Any classes,
which don't need to be used as callables).

There is a solution involving a Voodoo and an (unexported)
VoodooAttribute class which *can* solve this (Voodoo.__getattr__ returns
an instance of VoodooAttribute (subclass of Voodoo overloading
__call__), whose .__call__ method returns an instance of Voodoo), *but*
then you can't use:

Voodoo().startswith # this is a VoodooAttribute instance

as an argument to groupby, unless you use:

Voodoo(Voodoo().startswith) # this is a Voodoo instance

I can't think at the moment a case in the groupby context where the
latter would be needed (a VoodooAttribute behaves the same for
attributes that are not methods), but I'm sure many others will, and
even if they don't, it sure smells 'breakage'.

Unless the Voodoo class does what it does best (and I'll rename it to
Argument for clarity below), and an instance of it is the only argument
to the *explicit* initialisation of the FuncMaker class (the final
Voodoo :):

lambda x: x.startswith("text")

Too unpythonic perhaps?

PS attrgetter and itemgetter sound fine btw, I'm just trying to help
this brainstorming for more general solutions --if any.
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix

More information about the Python-Dev mailing list