How many languages features are available for OO support?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Fri May 17 10:55:39 EDT 2002


On Fri, 17 May 2002 13:41:00 +0100, "Pedro B. Gomes Costa"
<pbeck at ciberbit.pt> wrote:

>Hi
>
>> > Does Python support the notion of an abstract class or an interface
>> > through the use of language features?
>
>Here is a quote from the excelente book "Thinking in Python" from my
>favourite C++/Java and now Python book writer, Bruce Eckel
>(www.bruceeckel.com)
>
>"Because Python is weakly typed, it doesn't really care about interfaces -
>all it cares about is applying operations to objects (in fact, Java's
>interface keyword would be wasted in Python). This means that inheritance in
>Python is different from inheritance in C++ or Java, where you often inherit
>simply to establish a common interface. In Python, the only reason you
>inherit is to inherit an implementation - to re-use the code in the base
>class."
>
>Btw, you can download a electronic version of the book from the address
>above.
>
>Pedro Costa
>

I agree with what you say, but interfaces can serve another very
important purpose (VIP). Quoting from M. Pelletier interfaces PEP 245:

"There are many implied interfaces in Python, commonly referred to as
"protocols"."
"Currently determining those protocols is based on introspection, but
often that also fails.  For example, defining __getitem__ implies both a
sequence and a mapping (the former with sequential, integer keys).
There is no way for the developer to be explict about which protocols
the object intends to implement."
"There is no unified model for determining if an object can be used in a
certain, valid way."

That is, an interface specification could (should) serve to declare a
protocol for a given object, e.g. if a class implemented interface
IIterable (the objects are iterable) then a method __iter__ must be
declared in the class and with the right signature.

If for a moment we take an extensional definition of interface (= a set
of classes) then prolly it could be implemented using metaclasses,
although I haven't figured out *exactly* how. Some syntactic sugar could
then be added on top of this (a la PEP 245).

Anyway, what I would like to stress is that in Python one mostly codes
for/against behaviour (protocol) and not type, and that some form of
behaviour declaration (interface) would be highly desirable, IMHO.


All the best,
Gonçalo Rodrigues



More information about the Python-list mailing list