language design question

Paul Rubin http
Mon Jul 10 05:01:16 EDT 2006


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())



More information about the Python-list mailing list