The Python standard library and PEP8

Christian Heimes lists at cheimes.de
Sun Apr 19 15:46:46 EDT 2009


Emmanuel Surleau wrote:
> 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.

If you replace the term "OO" with "Java" in your posting, then I'm going
to agree with you. Neither Java nor Python are pure object oriented
languages. A while ago a friend of mine called Java a class centric
programming language. I think that describes Java's coding philosophy
very good. Python has a different coding philosphy. You can't assume
that Java style development works with Python (and vice versa).

Now to your rant about len(). Python class may implement several
protocols with magic methods like __len__(), __iter__() or
__getitem__(). The magic methods start and begin with two _. When a
method implements the __len__() method then it supports len(). In Python
readability counts. Try "import this" in a Python shell.

PEP 8 is a coding guideline for the standard library. It's not a law.
Some modules are older than the PEP, other modules and libraries were
developed outside the core and added later. We had a long discussion
about PEP8'ing more code but eventually we decided against it. More
chances would break too much 3rd party code.

Christian




More information about the Python-list mailing list