Why PEP 245??

nanotech at europa.com nanotech at europa.com
Wed May 30 18:26:34 CEST 2001


All:

First, let me start by saying if you believe I am not understanding
interfaces in general, please provide links and I will gladly return
with questions after having read them!!

Onward!

Matt:

    No standard way to disambiguate documentation
    of a class's interface from documentation about
    how blue the sky is, or what year it is, etc...

    There have been several occasions where I was
    able to see that a class implemented a
    well-understood interface and then not had to
    refer to the documentation anymore... I knew
    how it worked.

Alex:

    Code is preferable to comments (and docstrings)
    because it does not "get out of sync", or if it
    does that shows at once.

As it seems to be now, an Interface is similar to a class which has no
implementation, but *requires* doc-strings. But what keeps the
doc-strings meaningful?

Let me go out on a limb and post code (untested because I do not have
access to a build with interface implemented):


class square:
    def __init__(self):
        pass
    def show(self):
        "Doc-string specific to square.show"
        print """
*****
*   *
*   *
*****
"""

class circle:
    def __init__(self):
        pass
    def show(self):
        "Doc-string specific to circle.show"
        print """
 ***
*   *
*   *
 ***
"""

class triangle:
    def __init__(self):
        pass


Now we could add an interface:


interface ShowInterface:
    "Classes can _show_ themselves"

    def show():
        "Display ASCII representation of the object"


and I modify my class definition:


class square implements ShowInterface:

 <nothing else changes>

class circle implements ShowInterface:

 <nothing else changes>

class triangle implements ShowInterface:

 <nothing else changes>


But now what? What makes sure the doc-strings stay current or make
sense? Does a "ContractUnfulfilledError" occur when I create an
instance of 'triangle' because 'show' was never defined/implemented?
In short, what can I do now that I could not?

Thanks!!

  Quentin Crain







More information about the Python-list mailing list