Does Class implements Interface?

Max Landaeus max at landaeus.com
Fri Aug 28 01:36:33 EDT 2009


Emanuele D'Arrigo wrote:
> 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
>   
Check out Alex Martelli's recipe 'Checking wheter an object has 
necessary attributes' in the Python Cookbook:
http://books.google.co.uk/books?id=Q0s6Vgb98CQC&printsec=frontcover&dq=python+cookbook#v=onepage&q=&f=false 
<http://books.google.co.uk/books?id=Q0s6Vgb98CQC&printsec=frontcover&dq=python+cookbook#v=onepage&q=&f=false>

Max



More information about the Python-list mailing list