anonymous variable

Emmanuel Jeandel ejeandel at ens-lyon.fr
Sat Jul 7 11:15:03 EDT 2001


Hello

In a script i made, i have tuples (name, nb, size) in a list and i wanted to
sort the list with respect to size.

In python 1.5, i do : 
L.sort ( lambda (_,_, size1) (_,_,size2) : cmp(size1,size2))
_ is a variable that, according to the language reference manual,  has
no special meaning, and i find it adequate for meaning 'I don't use
this value'
I also write code such as (_,nb, size) = c, and so on

But, in python 2.2 (0501 snapshot), it gives an error, _ being use twice
in the definition of the fonction. I agree, but writing 
L.sort ( lambda (dum1, dum2, size1) (dum3, dum4, size2) : cmp (size1,size2))
is a bit too complicate for me.
and writing
L.sort ( lambda x y : cmp (x[2],y[2])) 
is not the solution because it is not as expressive as what i do before.

In the majority of fonctionnal and logic programming languages, you have
such a keyword, that says : 'We don't know what is its
value, but who cares ?'
Can we have the same in python ?

Thanks in advance
Emmanuel



More information about the Python-list mailing list