PEP for fp.type()

Alex Martelli aleax at aleax.it
Wed Jan 16 09:49:15 CET 2002


"Tim Cera" <timcera at earthlink.net> wrote in message
news:9c3f2e9c.0201151913.5d9f2736 at posting.google.com...
    ...
> 3) IANALL but it seems to make sense as a file method because it would
> return a characteristic of the file.  Whether it makes sense as a file

Note that this is not an optimal rationale to decide what's best
architected as an object's method, and what's best architected
as a function accepting an object as a parameter.  Consider
Python's built-in functions.  Doesn't id(), for example, "return
a characteristic" of the object that's passed to it?  Sure it
does, but, it would be wrong to make it a method of each object.

Rather, a designer should consider: am I willing to put the
burden of implementing this method on ANY object that wants
to be an "X-like" object?  How important is it for this method
to be overridable by specific object types/classes, and does
it have an obvious implementation in terms of already existing
primitives?  See the xreadlines module AND method of file
objects for one possible compromise design.


> method or an os method I don't think is the most important thing.

Probably not, but it IS one important architectural decision -- and
I think this is one more case in which an external function is best.

Haskell typeclasses let methods be added post-facto with default
implementations in terms of other methods, but still overridable
(indeed in a typical case, a typeclass's methods are by default
defined in term of each other and at least one must be overridden
to "break the dependency cycle").  Python doesn't have such a
smooth mechanism for this, although a metaclass and/or mix-in could
be used to emulate it reasonably closely ("protocol adaptation" as
proposed in an open PEP would IMHO be the Pythonic way to make
this sufficiently general).

Thus, the method-vs-function choice is important in designing
for Python (particularly when thinking of functionality meant
to get into core Python).  MHO is that a PEP for a new method
of builtin file objects would stand no chance, while one for a
new separate module to be added to the standard library might.


Alex






More information about the Python-list mailing list