Anonymus functions revisited

bruno modulix onurb at xiludom.gro
Tue Mar 22 11:12:35 CET 2005

Claudio Grondi wrote:
> For me, one of the reasons for using Python
> is the ease and the intuivity of reading its
> code.

I guess this is true for many of us. Now 'ease and intuitivity' stronly 
depends on one's background...

> I have  a problem with intuitively getting
> what is going on when using a pattern like
>   (x,y,z=0) -> (x,y,z)
> where I expect at the first glance some
> C code with access to class members.

There are nothing like a class or a class member in C.

> At least I must start looking at the code
> more closely thinking why is someone
> using such a construct.

> Already
>   lambda x,y,z=0:(x,y,z)
> is a problem for me.

Not for me. It's clearly a function that takes 2 mandatory and one 
optional argument (which default to 0), and returns a tuple of it's 

it's named equivalent is:
def make_three_tuple(x, y, z=0):
   return (x, y, z)

> Why not:
> try:
>   (x,y,z)
> except NameError:
>   z=0
>   (x,y,z)
> ?

Because it does not do the same thing ?-)

Because, if it did, it would still require 5 times more lines of code to 
do the same thing (hence it's more error-prone by a factor a 5) ?

Because it uses an exception, which is far less efficient than using a 
default argument ?

Because the name error could be on x and/or y too, and the try:except 
clause would hide it ?

I stop here, but I think I could think of more good (IMHO) reason to 
prefer the lambda form.

> Watching the last piece of code
> can even directly be seen, that there
> is eventually a NameError
> problem with z to handle,

No. AFAICT, the name error could be with x and/or y and/or z.

> so where is the gain of using
> lambda or the mapping?

See upper...

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