The Python standard library and PEP8

Gabriel Genellina gagsl-py2 at
Sun Apr 19 21:55:19 CEST 2009

En Sun, 19 Apr 2009 15:41:02 -0300, Emmanuel Surleau  
<emmanuel.surleau at> escribió:
> On Sunday 19 April 2009 19:37:59 Gabriel Genellina wrote:
>> En Sun, 19 Apr 2009 13:43:10 -0300, Emmanuel Surleau

>> > On an unrelated note, it would be *really* nice to have a length
>> > property on
>> > strings. Even Java has that!
>> Why would it be nice to have? I never missed it...
> First off, it's pretty commonplace in OO languages. Secondly, given the
> number of methods available for the string objects, it is only natural to
> assume that dir("a") would show me a len() or length() or size() method.
> Having to use a function for such a mundane operation feels unnatural and
> not OO.

Perhaps in statically typed languages. Python is dynamic, so a x.length()  
requires a method lookup and that's expensive. len(x) on the contrary, can  
be optimized on a case by case basis -- it DOESN'T translate to  
x.__len__() as some might think.

On a side note, there is an alternative to dir(), more human-friendly:

py> see("a")
   ?   []   in   +   *   %   <   <=   ==   !=   >   >=   len()    
   .center()   .count()   .decode()   .encode()   .endswith()    
   .find()   .format()   .index()   .isalnum()   .isalpha()   .isdigit()
   .islower()   .isspace()   .istitle()   .isupper()   .join()   .ljust()
   .lower()   .lstrip()   .partition()   .replace()   .rfind()   .rindex()
   .rjust()   .rpartition()   .rsplit()   .rstrip()   .split()    
   .startswith()   .strip()   .swapcase()   .title()   .translate()    

You can see len() there.

Gabriel Genellina

More information about the Python-list mailing list