newbie-question: interfaces

Patrick W xnexau at yahoo.com.au
Sat Apr 13 22:42:58 EDT 2002


"logistix" <logstx at bellatlantic.net> writes:

> Interfaces are important if you want to enforce a design-by-contract.
> Granted, in Python you can query a class at runtime to see if it implements
> a property or method, but this doesn't guarantee that the method does what
> you want it to do semantically.  (Just because a function is named foo, it
> doesn't mean it foos the way you want it to.)
> 
> Not to be a prick either, but if you're not using interfaces or ABC's, then
> you're not taking advantage of the true goals of OO programming, you're just
> warping the function names and data structures for your convienence.  The
> whole point of OO is to deal through abstract interfaces that hide the
> implementation details.


I agree that interfaces / abstract base classes are important in
statically typed OO languages, but I still don't see what purpose they
serve in dynamically typed languages like Python. It seems to me that
object orientation has a somewhat different 'flavour' in the two types
of languages, and one of the main differences lies in the purpose that
inheritance serves.

If we use C++ as an example of a statically typed OO language, you
might use inheritance for two reasons. One would be to specify an
interface (guaranteeing that any instance of a class derived from a
certain type will implement a certain method which can be called
without knowing the exact (sub)type of an object). Another purpose
is to simply inherit behaviour, which is basically a convenient code
reuse mechanism.

I can see good uses for implementation inheritance in Python, but not
for interface inheritance. In your remarks above, you imply that there
is a choice in Python between providing an abstract interface, or
doing explicit type checking. But it seems to me that this overlooks
the fact that Python methods and functions are similar to C++
templates. If an object supports a particular behaviour, regardless of
whether and where it inherits this behaviour from, you can invoke
it. If it doesn't, you can't. Since none of this must (or can) be
handled at "compile time" in Python, what would be the advantage of an
forcing objects to inherit from an abstract interface?

It would seem to me that this would even have a (slight)
disadvantage. Eg. by effectively hard-wiring a function or method to
deal with only objects that adhere to a certain _inherited_ "fooable"
interface, you prevent it from handling an object that is "fooable" in
its own right, without inheriting that behaviour from somewhere
else. (Which leads me to suspect that this is the whole point ;-)
Perhaps we're just on different sides of the fence as to whether this
is desirable or not?)

Cheers,
P.



More information about the Python-list mailing list