language design question

Steven Bethard steven.bethard at gmail.com
Mon Jul 10 12:30:53 EDT 2006


Paul Rubin wrote:
> Steven Bethard <steven.bethard at gmail.com> writes:
>> If len() were a method of string objects, you could try using the
>> unbound method and writing this as::
>>
>>      >>> sorted(['aaa', 'bb', 'c'], key=str.len)
>>      ['c', 'bb', 'aaa']
>>
>> But then your code would break on lists that weren't strings.
> 
> sorted(['aaa', 'bb', 'c'], key=lambda x: x.len())

Of course, but all you're doing with the lambda is creating the len() 
function.

STeVe



More information about the Python-list mailing list