Does Class implements Interface?

Discussion in 'Python' started by Emanuele D'Arrigo, Aug 27, 2009.

  1. Greetings everybody,

    let's say I have a Class C and I'd like to verify if it implements
    Interface I. If I is available to me as a class object I can use
    issubclass(C, I) and I can at least verify that I is a superclass of
    C. There are a couple of issues with this approach however:

    1) C might override not just I's methods code but also I's methods
    signatures, effectively changing the interface.
    2) What if I is -not- available to me as the superclass of C but as a
    description of the interface, i.e. as an IDL(*) file?

    Are there resources such as tools, recipes, documents or strategies
    that could help me deal with these issues? I've already looked into
    the ABC module and the zope.interface. I'm just fishing for more
    things to look at.

    Manu

    (*) http://en.wikipedia.org/wiki/Interface_description_language
     
    Emanuele D'Arrigo, Aug 27, 2009
    #1
    1. Advertising

  2. On Aug 27, 6:16 am, "Emanuele D'Arrigo" <> wrote:
    > Greetings everybody,
    >
    > let's say I have a Class C and I'd like to verify if it implements
    > Interface I. If I is available to me as a class object I can use
    > issubclass(C, I) and I can at least verify that I is a superclass of
    > C. There are a couple of issues with this approach however:
    >
    > 1) C might override not just I's methods code but also I's methods
    > signatures, effectively changing the interface.
    > 2) What if I is -not- available to me as the superclass of C but as a
    > description of the interface, i.e. as an IDL(*) file?
    >
    > Are there resources such as tools, recipes, documents or strategies
    > that could help me deal with these issues? I've already looked into
    > the ABC module and the zope.interface. I'm just fishing for more
    > things to look at.
    >


    Have you heard of duck typing?

    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.
     
    Jonathan Gardner, Aug 27, 2009
    #2
    1. Advertising

  3. On Aug 27, 9:42 pm, Jonathan Gardner <>
    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
     
    Emanuele D'Arrigo, Aug 27, 2009
    #3
  4. On Aug 27, 3:09 pm, "Emanuele D'Arrigo" <> wrote:
    >
    > Apologies, my fault,


    No apology is necessary.

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


    Don't bother with introspection using hasattr() and such. Use
    exceptions. If the object that is instantiated at run-time doesn't
    provide the right interface, throw an exception and direct it to the
    responsible party.

    This isn't much different than what you'd do if you're writing an app
    that expects a string of digits but is instead given a string of non-
    digits. Rather than inspecting the string before converting it
    (introspection), just try to convert it and catch the exception. Then
    handle the exception such that you tell the person who entered the
    data, "I wanted numbers, not letters" and allow them to try again.

    When you stop asking "Did I get something I expect?" except only when
    it is absolutely necessary, then you can get back to writing your
    program.
     
    Jonathan Gardner, Aug 27, 2009
    #4
  5. On Aug 27, 3:09 pm, "Emanuele D'Arrigo" <> wrote:
    > On Aug 27, 9:42 pm, Jonathan Gardner <>
    > wrote:
    >
    > > Have you heard of duck typing?

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


    As a joke (but only partly), here's the python code you'd use to check
    to see if a value conforms with expectations:

    <code>
    </code>

    As you can see, the above code is not only easy to write, but easy to
    write unit tests for, compile, run, and debug. It simply doesn't get
    better than this.
     
    Jonathan Gardner, Aug 27, 2009
    #5
  6. Emanuele D'Arrigo

    Max Landaeus Guest

    Emanuele D'Arrigo wrote:
    > On Aug 27, 9:42 pm, Jonathan Gardner <>
    > 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=...cover&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
     
    Max Landaeus, Aug 28, 2009
    #6
  7. Jonathan, Stephen and Max, thank you all for the tips and tricks. Much
    appreciated.

    Manu
     
    Emanuele D'Arrigo, Aug 28, 2009
    #7
  8. On Aug 27, 2009, at 9:16 AM, Emanuele D'Arrigo wrote:

    > Are there resources such as tools, recipes, documents or strategies
    > that could help me deal with these issues? I've already looked into
    > the ABC module and the zope.interface. I'm just fishing for more
    > things to look at.


    You say above that you looked at zope.interface.
    Did you look at zope.interface.verify?
    Specifically, verify.txt doctests state that:

    "An object provides an interface if

    - either its class declares that it implements the interfaces,
    or the object declares that it directly provides the interface

    - the object defines all the methods required by the interface

    - all the methods have the correct signature

    - the object defines all non-method attributes required by the
    interface"

    zope.interface.verify.verifyObject(I, obj) will check all of the above.
    Similarly, zope.interface.verify.verifyClass(I, C) will check the class.
    It is a good practice to call these functions in the regression tests.

    If this is not what you are looking for, sorry for the noise.

    Zvezdan
     
    Zvezdan Petkovic, Aug 28, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. owais
    Replies:
    3
    Views:
    1,055
    Francois Bonin [C# MVP]
    Jun 23, 2005
  2. Jordan Smith

    Implements and Interface

    Jordan Smith, Nov 27, 2003, in forum: Java
    Replies:
    3
    Views:
    396
    Joona I Palaste
    Nov 28, 2003
  3. Replies:
    8
    Views:
    538
  4. Bruno Desthuilliers

    Python "implements <interface>" equivalent?

    Bruno Desthuilliers, Oct 3, 2007, in forum: Python
    Replies:
    11
    Views:
    611
    David
    Oct 5, 2007
  5. Zuisman Moshe
    Replies:
    24
    Views:
    2,984
    Zuisman Moshe
    Apr 21, 2009
Loading...

Share This Page