Does Class implements Interface?

Stephen Fairchild somebody at somewhere.com
Thu Aug 27 18:43:30 EDT 2009


Emanuele D'Arrigo wrote:

> 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.

http://docs.python.org/library/abc.html

In particular, pay attention to the term virtual subclass and also note in
the second code example the use of __subclasshook__ to define virtual
subclass membership by the presence (or not) of class attributes.
-- 
Stephen Fairchild



More information about the Python-list mailing list