My python annoyances so far

Kay Schluehr kay.schluehr at
Thu Apr 26 21:55:00 CEST 2007

On Apr 26, 6:07 pm, fli... at wrote:

> Well, why do some things in the library have to be functions, and
> other things have to be class methods?
> Why aren't they all just either functions or class methods? like
> perhaps ruby.

A good question. Part of the answer might be that their are no
abstract base classes / interfaces in the language so far. These have
a particular meaning and you might ask whether some object has certain
abilities like being "sizable". With ABCs or interfaces it feels
natural to implement abstract methods in subclasses. Without them you
can either add methods ad hoc, without any conceptual background and
consistency requirements of your model, or you are going to implement
interface constraints in terms of so called "protocols". A protocol is
a sequence of actions which require interfaces to be present ( they
are abstract algorihms / command patterns ). Special functions
( __add__, __len__, etc. ) are the mediators of protocols. For
instance the meaning of len() can be understood in terms of protocols:

def len(obj):
      return obj.__len__()

Other protocols are more implicit e.g. those that couple operators to
method calls. You already mentioned special methods for arithmetic
operations. I sometimes like to think that Python is a language
developed around protocols, and they have a strong backing in the
language. With Py3K Python will become a bit more Java like, at least
with regard to the supported language constructs.


More information about the Python-list mailing list