Anonymus functions revisited

bruno modulix onurb at xiludom.gro
Tue Mar 22 18:56:03 CET 2005

Claudio Grondi wrote:
>>What do you find most readable: your version, with an ad-hoc function
>>defined somewhere else, far away in the code, or a simpler:
>>for (x,y,z=0) in tupleList:
>>    do_whatever_with_it()
> I came up with the "ad-hoc function"
> to give a kind of replacement
> for the used syntax, in order
> not to ask why not immediately:
> for tuple in tupleList:
>   do_whatever_with_it()
> ?

What if do_whatever_with_it() waits for a triplet, we have a list of 
mixed pairs and triplets, and augmenting pairs with a 0 would do for us?

As I said, there's always a way to do it as long as the language is 
turing complete. The problem is with is of use and readiblity - 
expressive power - which, I agree, is at least somewhat subjective !-)

> Sure I like the syntax as long as
> I think in terms of unpacking
> tuples, but at the first glance
> I thought that the intention
> was to get only x, y from the tuple
> setting all z in the yielded tuples
> to zero (as suggested in this
> thread by giving the example of
> going from 3D down to 2D where
> with all z=0 the algorithm will
> remain the same).
> What about
> "for (x,y,if z not in tupleList: z=0 else: z=1) in tupleList"
> ?

Should be:
for x, y, if [???]:z=0 else:z=1 in tupleList:
   # etc

This Would do for me. But since we're not going to have a ternary 
woperator (or everything's-an-expression for that matter)...

> What if the tupleList is empty?
No iteration.

> Should existing z be set to zero
> or not after the line with the
> for loop was executed?

should follow the same rule as for other vars.

> Should
> for (,y) in tupleList:
>  do_whatever_with_it()
> give only the y and
> for (,,z) in tupleList:
> only the z values?

Don't know. Didn't think of it.

> If it should become possible to
> use the "for (x,y,z=0) in tupleList"
> syntax, I would sure like also to have:
> "for (,,z) in tupleList"

Sounds coherent... but then you already have:
for dont_care, ignore, y in tupleList:
   # etc

> "for (x,y,if z not in tupleList: z=0 else: z=1) in tupleList"
> "for (x,y,if z not in tupleList: z=0 else: z=2*x*z) in tupleList"
> and sure also:
> "for (x,y,z=0, *r) in tupleList"
> etc.
> My only objections are:
> Is it worth the effort?
> Would it not be confusing?
> Maybe starting with e.g. a
>   xlistcomprehension
> module where
> xlistcomprehension.execute(
> "[for (x,y,if z not in tupleList: z=0 else: z=1) in tupleList]"
> )
> or just xl(...) to keep it short
> provides the desired functionality
> is a good idea?
> Then it can be seen if this module
> becomes widely used and therefore
> worth to be integrated?

I don't think this would be possible. What is proposed requires 
modification to the language itself AFAICT.

> I think it would be nice to have, but I
> can't imagine it to be really very helpful.

I once had the same feeling about first class functions, higher-order 
functions, lambdas, list comprehension, __call__ special method, 
descriptors, metaclasses, and a whole lot of other constructs for that 
matter. Now I woul be very unhappy not to have them !-)

bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for 
p in 'onurb at xiludom.gro'.split('@')])"

More information about the Python-list mailing list