Why this behavior? (was: Stupid Lambda Tricks)

Jonathan Gardner jgardn at alumni.washington.edu
Thu Feb 21 07:20:22 EST 2002


< snip excellent lambda tricks >
> 
> Many list functions return None, so you cannot easily use them in a
> stacked expression inside lambda. Examples of this are [].sort() and
> [].reverse(). Instead of writing:
> 
> list.some_function_that_returns_None()
> function(list)
> 
> you can write
> 
> function((list.some_function_that_returns_None(),list)[1])
> 
> Here is an example:
> 
> import random
> 
> sort = lambda x: (x.sort(),x)[1]
> reverse  = lambda x: (x.reverse(),x)[1]
> 

I've always wondered:

>>> "abc".upper()
"ABC"
>>> [3,1,2].sort()
< nothing >

Why is that? Is it just easier to implement it this way, or is there some 
sound philosophical reason for this?

Jonathan




More information about the Python-list mailing list