Does Class implements Interface?

Emanuele D'Arrigo manu3d at gmail.com
Thu Aug 27 18:09:18 EDT 2009


On Aug 27, 9:42 pm, Jonathan Gardner <jgard... at jonathangardner.net>
wrote:
> Have you heard of duck typing?

Yes.

> Ignore all those things and rely on human (aka natural language)
> documentation. That is, if you want to see if a class will work for an
> interface, go read the docs on the interface (or rather, what the
> function expects the interface to be) and what the class provides and
> see if they fit.

Apologies, my fault, I didn't explain that humans are out of the loop
entirely. It's only at runtime that the program obtains the class
object that might or might not conform to an expected interface. In
fact the program might obtain multiple objects and have to decide
which one conforms best to the expected interface. Of course I could
use hasattr() and the inspect module (anything else?) to find out if
existing attributes and method signatures are those expected. Or I
could just deal with the exceptions as they are raised. However, if I
could somehow rely that the object I'm getting has the right methods
and signatures I could avoid peppering the code with try/except
constructs.

I was just wondering then if this has been somewhat dealt with and has
been wrapped in a neat package, set of functions, recipe or pattern.

Manu



More information about the Python-list mailing list