Does Python support interfaces?

Chris Herborth chrish at pobox.com.invalid
Tue Jul 18 12:37:10 EDT 2000


According to Eric Hopper <hopper at omnifarious.mn.org>:
> In article <vndlmz0b5jp.fsf at camelot-new.ccs.neu.edu>, Justin Sheehy
> <dworkin at ccs.neu.edu> wrote:
> > 
> > Just define the class, and make sure that it provides the proper
> > interface.  Note that I'm not using "interface" with any special
> > language-dependent meaning here.  If it behaves properly, it behaves
> > properly, and that is all that matters.
> 
> Whee!  Inheritance by mysteriously coinciding method names is so much fun!

Feh:

from exceptions import TypeError

class Interface:
    def __init__( self ):
        if self.__class__ == Interface:
            # I'm an abstract base class, don't do that.
            raise TypeError

    def SomeMethod( self ):
        raise TypeError

class foo( Interface ):
    def __init__( self ):
        Interface.__init__( self )

    def SomeMethod( self ):
        print "feh!"

a = Interface() # raises an exception
b = foo()       # works like a charm

-- 
----------========================================================----------
Chris Herborth, DNRC Holder of Past Knowledge     Arcane Dragon -==(UDIC)==-
BeOS hacker, Catharon Productions, Inc.             http://www.catharon.com/
CTO, Next Generation Entertainment                     http://www.ngent.com/



More information about the Python-list mailing list