The Python standard library and PEP8
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Sun Apr 19 15:55:19 EDT 2009
En Sun, 19 Apr 2009 15:41:02 -0300, Emmanuel Surleau
<emmanuel.surleau at gmail.com> 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.
See
http://www.python.org/doc/faq/general/#why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list
On a side note, there is an alternative to dir(), more human-friendly:
http://inky.github.com/see/
py> see("a")
? [] in + * % < <= == != > >= len()
.capitalize()
.center() .count() .decode() .encode() .endswith()
.expandtabs()
.find() .format() .index() .isalnum() .isalpha() .isdigit()
.islower() .isspace() .istitle() .isupper() .join() .ljust()
.lower() .lstrip() .partition() .replace() .rfind() .rindex()
.rjust() .rpartition() .rsplit() .rstrip() .split()
.splitlines()
.startswith() .strip() .swapcase() .title() .translate()
.upper()
.zfill()
You can see len() there.
--
Gabriel Genellina
More information about the Python-list
mailing list