Python Equivalent to Java Interfaces?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Kazian

    #1

    Python Equivalent to Java Interfaces?

    I want to insure that all subclasses implement a certain method, but could
    not find anything that would do this for me. Is there anyway in Python to
    implement this check? Thanks!


  • Terry Hancock

    #2
    Re: Python Equivalent to Java Interfaces?

    On Friday 08 April 2005 11:12 pm, Brian Kazian wrote:[color=blue]
    > I want to insure that all subclasses implement a certain method, but could
    > not find anything that would do this for me. Is there anyway in Python to
    > implement this check? Thanks![/color]

    I understand there are two interface implementations in Python, one
    for Zope (in particular, the Zope 3 code has an implementation that
    can be rather easily separated out if you don't want all of Zope),
    and something called PyProtocols. Obviously, I only know my way
    around the Zope version, but it's pretty good.

    You realize of course, that if you really only wanted to check for a particular
    method, you can easily do this without extra help:

    if not (hasattr(myclas s, mymethod) and iscallable(myme thod)):
    raise MyInvalidClassE rror

    or something like that. The interface modules make it easier to automate
    much more complicated checks for lots of methods, etc., and are quite
    useful when you have lots of interfaces to keep straight.

    Cheers,
    Terry


    --
    Terry Hancock ( hancock at anansispacework s.com )
    Anansi Spaceworks http://www.anansispaceworks.com

    Comment

    • George Sakkis

      #3
      Re: Python Equivalent to Java Interfaces?

      "Brian Kazian" wrote:
      [color=blue]
      > I want to insure that all subclasses implement a certain method, but[/color]
      could[color=blue]
      > not find anything that would do this for me. Is there anyway in[/color]
      Python to[color=blue]
      > implement this check? Thanks![/color]


      Check out PyProtocols (http://peak.telecommunity.com/PyProtocols.html).
      Protocols in a way extend (no pun intended) typical interfaces by
      giving more flexibility to independent components to talk to each
      other.

      George

      Comment

      • gene.tani@gmail.com

        #4
        Re: Python Equivalent to Java Interfaces?

        There's 4 places to look: zope, twisted, PEAK and pyProtocols: this
        link describes pyprotocols pretty well

        http://peak.telecommunity.com/protoc...protocols.html
        http://peak.telecommunity.com/PyProtocols.html

        Comment

        • Michele Simionato

          #5
          Re: Python Equivalent to Java Interfaces?

          Google for "Hettinger interface checking": the first hit
          is the cookbook recipe you are looking for.

          Michele Simionato

          Comment

          Working...