subclassing str

Mark Wooding mdw at distorted.org.uk
Wed Nov 10 06:56:08 EST 2010


Lawrence D'Oliveiro <ldo at geek-central.gen.new_zealand> writes:

> In message <87lj52kwln.fsf.mdw at metalzone.distorted.org.uk>, Mark Wooding 
> wrote:
>
> > One option is to implement a subclass which implements the additional
> > protocol.
>
> This is why I think object orientation ruins your ability to think
> properly.  For “protocol” read “function”. If you want to implement a
> new function, then implement a new function, why do you have to go
> through this “subclassing” malarkey just to do something so simple.

Functions don't do type-dependent dispatch, which was, of course, the
whole point.

A `protocol' is a collection of messages understood by a number of
different kinds of objects, causing potentially object-specific
behaviour.  I gave the explicit example of serialization: string
probably serializes differently from an integer or a list.  The
`__str__' and `__repr__' methods form a simple protocol for object
printing, as an additional example.  The `str' and `repr' functions
provides a potentially useful external entry points into the protocol,
but they work by doing a type-dependent dispatch on the argument --
calling the appropriate method.

Object orientation isn't useless or an impediment to clear thinking.  It
does seem to have turned into a bizarre kind of religion by some, and
many `mainstream' languages provide very poor object orientation
features (yes, Java and C#, I'm looking at you).  But OO can be useful.

I think the notion of `protocol' is central to coherent OO design, but
this seems largely overlooked in much of the literature I've read.  This
may mean that many people are muddled about what OO is actually for.

-- [mdw]



More information about the Python-list mailing list