The Python standard library and PEP8

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Apr 20 02:54:02 EDT 2009


Emmanuel Surleau:
> On an unrelated note, it would be *really* nice to have a length property on
> strings. Even Java has that!

Once you have written a good amount of Python code you can understand
that a len() function, that calls the __len__ method of objects, is
better. It allows you to write:
sorted(seq, key=len)

Instead, if you have a len attribute you need:
sorted(seq, key=attrgetter("len"))
Or even:
sorted(seq, key=lambda x: x.len)

Bye,
bearophile



More information about the Python-list mailing list