Missing interfaces in Python...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Smith

    #31
    Re: Missing interfaces in Python...

    In article <1he0aen.324fes 1th8n52N%aleaxi t@yahoo.com>,
    aleaxit@yahoo.c om (Alex Martelli) wrote:
    [color=blue]
    > Roy Smith <roy@panix.co m> wrote:
    >[color=green]
    > > Peter Maas <peter.maas@som ewhere.com> wrote:[color=darkred]
    > > > He probably means that with interfaces one could test compliance
    > > > with the interface as a whole instead of testing each member and
    > > > each signature as a single piece.[/color]
    > >
    > > All interfaces (as implemented by Java) prove is that your class has a
    > > bunch of methods with the right names and signatures. It doesn't
    > > prove that those methods do the right things. It's like having a[/color]
    >
    > There's a _tiny_ bit more -- accidental clashes of methodnames are more
    > likely to be avoided. I.e.,
    >
    > interface ILottery {
    > void draw();
    > };
    >
    > interface IGunslinger {
    > void draw();
    > };
    >
    > interface IPainter {
    > void draw();
    > };
    >
    > A class asserting, e.g., "implements IPainter", doesn't thereby risk
    > being accidentally misused where an IGunslinger is required (OTOH,
    > implementing >1 of these IS a bother, but that's sort of inevitable).[/color]

    I suppose, but all you've really done is move the problem to a different
    namespace. Which IPainter did you have in mind? The one that has to do
    with people who apply oils to canvas, or the one that has to do with ropes
    that are used to tie up rowboats?

    Comment

    • Neal Becker

      #32
      Re: Missing interfaces in Python...

      redefined.horiz ons@gmail.com wrote:
      [color=blue]
      > I'm coming from a Java background, so please don't stone me...
      >
      > I see that Python is missing "interfaces ". The concept of an interface
      > is a key to good programming design in Java, but I've read that they
      > aren't really necessary in Python. I am wondering what technique I can
      > use in Python to get the same benefits to a program design that I would
      > get with interfaces in Java.
      >
      > For example, if I want to have a program with a Car object, and a Bus
      > object. I want both of these objects to present a common group of
      > methods that can be used by Mechanic objects, but slightly different
      > methods that can be used by Driver objects.
      >
      > In Java I would accomplish this by defining an IFixable interface that
      > would be implemented by both the Car and Bus objects. Mechanic objects
      > would work with any object implementing this interface.
      >
      > How would I approach this problem in Python? I think I would use an
      > abstract class instead of an interface for IFixable, since Python
      > supports multiple inheritance, but I'm not sure this is correct.
      >
      > Thanks for any suggestions.
      >[/color]

      I see various answers that Python doesn't need interfaces. OTOH, there are
      responses that some large Python apps have implemented them (e.g., zope).
      Does anyone have an explanation of why these large systems felt they needed
      to implement interfaces?

      Comment

      • Alex Martelli

        #33
        Re: Missing interfaces in Python...

        Roy Smith <roy@panix.co m> wrote:
        ...[color=blue][color=green]
        > > A class asserting, e.g., "implements IPainter", doesn't thereby risk
        > > being accidentally misused where an IGunslinger is required (OTOH,
        > > implementing >1 of these IS a bother, but that's sort of inevitable).[/color]
        >
        > I suppose, but all you've really done is move the problem to a different
        > namespace. Which IPainter did you have in mind? The one that has to do
        > with people who apply oils to canvas, or the one that has to do with ropes
        > that are used to tie up rowboats?[/color]

        In Java's excellent naming convention for modules, there's no ambiguity:
        I specifically requested it.aleax.artist s.IPainter, or else specifically
        requested com.panix.roy.b oating.IPainter -- nothing stops any language
        with a structured namespace for modules from using that convention.


        Alex

        Comment

        • Roy Smith

          #34
          Re: Missing interfaces in Python...

          Alex Martelli <aleaxit@yahoo. com> wrote:[color=blue]
          >Roy Smith <roy@panix.co m> wrote:
          > ...[color=green][color=darkred]
          >> > A class asserting, e.g., "implements IPainter", doesn't thereby risk
          >> > being accidentally misused where an IGunslinger is required (OTOH,
          >> > implementing >1 of these IS a bother, but that's sort of inevitable).[/color]
          >>
          >> I suppose, but all you've really done is move the problem to a different
          >> namespace. Which IPainter did you have in mind? The one that has to do
          >> with people who apply oils to canvas, or the one that has to do with ropes
          >> that are used to tie up rowboats?[/color]
          >
          >In Java's excellent naming convention for modules, there's no ambiguity:
          >I specifically requested it.aleax.artist s.IPainter, or else specifically
          >requested com.panix.roy.b oating.IPainter -- nothing stops any language
          >with a structured namespace for modules from using that convention.[/color]

          This is true. This is one of the things Java does well.


          Comment

          • Alex Martelli

            #35
            Re: Missing interfaces in Python...

            Ben <groups@theyoun gfamily.co.uk> wrote:
            ...[color=blue]
            > It seems to me that a lot of python projects reimplement interfaces or
            > adaption of some kind once they reach a certain size (Zope, PEAK, eggs,
            > TurboGears, etc), which implies that they really do have some benefits,
            > particularly in documentation.[/color]

            PEAK is an interesting counterexample, particularly since Philip Eby
            tends to be "ahead of the curve": it appears that he's determined that
            ``generic functions'' (with ``multimethods' ') are a superior approach,
            and seems to have convinced Guido to the point that GFs are the
            alternative being actively explored for Py3k. I will admit that, while
            still undecided, I'm halfway-convinced enough not to mourn for PEP 246.

            ((It's possible to implement protocols, a la PyProtocols and as
            advocated in PEP 246, in terms of GFs, or viceversa; in PEAK, it's
            GFs-in-terms-of-protocols for strictly historical reasons, but the
            advantage along various axes seems to be to the
            protocols-in-terms-of-GFs camp; since protocols are a strict superset of
            interfaces...))

            BTW, this _could_ be seen as yet another case of "reimplemen ting LISP",
            since Lisp/CLOS was always based on GFs/multimethods... ;-).


            Alex

            Comment

            • Rene Pijlman

              #36
              Re: Missing interfaces in Python...

              Alex Martelli:[color=blue]
              >PEAK is an interesting counterexample, particularly since Philip Eby
              >tends to be "ahead of the curve":[/color]

              I never noticed PEAK before. Is it well worth studying?

              Comment

              • Rene Pijlman

                #37
                Re: Missing interfaces in Python...

                Neal Becker:[color=blue]
                >I see various answers that Python doesn't need interfaces. OTOH, there are
                >responses that some large Python apps have implemented them (e.g., zope).
                >Does anyone have an explanation of why these large systems felt they needed
                >to implement interfaces?[/color]

                A programming language doesn't need interfaces, unless it insists on
                compile time checking of just about everything.

                The idea of interfaces arises from the construction and maintenance of
                large and complex software systems. It provides a level of abstraction
                that makes it easier to talk about a component and document what it
                requires from and offers to it's environment.

                Also, interfaces can make this documentation first-class objects, so test
                tools, IDE's and software design tools can take advantage of it.

                Comment

                • bruno at modulix

                  #38
                  Re: Missing interfaces in Python...

                  Neal Becker wrote:
                  (snip)[color=blue]
                  > I see various answers that Python doesn't need interfaces. OTOH, there are
                  > responses that some large Python apps have implemented them (e.g., zope).
                  > Does anyone have an explanation of why these large systems felt they needed
                  > to implement interfaces?[/color]

                  These "interface" ("protocol" would be a better name IMHO) systems are
                  not exactly the same as Java's interfaces. They are mainly used a a way
                  to describe components and allow for component adaptation and
                  substitutabilit y. Think of it as a highly decoupled pluggable component
                  system, not as a language-level subtyping mechanism. BTW, you'll notice
                  that these projects (Zope, Twisted, PEAK, ...) are mostly large frameworks.

                  My 2 cents...
                  --
                  bruno desthuilliers
                  python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                  p in 'onurb@xiludom. gro'.split('@')])"

                  Comment

                  • Alex Martelli

                    #39
                    Re: Missing interfaces in Python...

                    Rene Pijlman <reply.in.the.n ewsgroup@my.add ress.is.invalid > wrote:
                    [color=blue]
                    > Alex Martelli:[color=green]
                    > >PEAK is an interesting counterexample, particularly since Philip Eby
                    > >tends to be "ahead of the curve":[/color]
                    >
                    > I never noticed PEAK before. Is it well worth studying?[/color]

                    Oh yes.


                    Alex

                    Comment

                    • Carl Banks

                      #40
                      Re: Missing interfaces in Python...

                      Ben wrote:[color=blue]
                      > It seems to me that a lot of python projects reimplement interfaces or
                      > adaption of some kind once they reach a certain size (Zope, PEAK, eggs,
                      > TurboGears, etc), which implies that they really do have some benefits,
                      > particularly in documentation.[/color]

                      Yes. On my current largish project I've noticed that, as I refactor
                      code and make it more and more concise, my classes have aquired pretty
                      mature interfaces. I've begun thinking that a little inline tool to
                      check compliance would be helpful at this point.

                      However, all that refactoring I did to get my classes to that point
                      would have been much more tedious if I'd been using interface
                      definitions in early development; changing the interface definition
                      every time some new problem came up would have been a pain. So I'm
                      leery of having them in the languge even if we're not forced to use
                      them. I'm afraid people will start writing interface definitions
                      first, when they should be taking advantage of the duck typing to allow
                      easy design changes when necessary. Only after a lot of effort, a lot
                      of refactoring, and a large part of the problem space explored, will
                      the interfaces be mature enough that writing interface definitions
                      would be useful and not a burden. (And let's face it: there aren't
                      many projects that get that far. :)

                      Adaptation I have no comment about, not having needed it (yet).


                      Carl Banks

                      Comment

                      • Ben

                        #41
                        Re: Missing interfaces in Python...

                        Oh I agree entirely. They are just equivalent ways of managing the
                        complexity of large projects.

                        I guess interfaces are "providing" specifications, and generics are
                        "receiving" specifications, so single dispatch methods can be identical
                        to interfaces only "inverted".

                        Therefore, as there is no interface equivalent of full multi-methods,
                        it shows that multimethods are the only primative you need to implement
                        all of the above

                        Cheers,
                        Ben

                        Comment

                        • Michele Simionato

                          #42
                          Re: Missing interfaces in Python...

                          Carl Banks wrote:[color=blue]
                          > Only after a lot of effort, a lot
                          > of refactoring, and a large part of the problem space explored, will
                          > the interfaces be mature enough that writing interface definitions
                          > would be useful and not a burden. (And let's face it: there aren't
                          > many projects that get that far. :)[/color]

                          +1 QOTW

                          Michele Simionato

                          Comment

                          • bruno at modulix

                            #43
                            Re: Missing interfaces in Python...

                            bruno at modulix wrote:[color=blue]
                            > Neal Becker wrote:
                            > (snip)
                            >[color=green]
                            >>I see various answers that Python doesn't need interfaces. OTOH, there are
                            >>responses that some large Python apps have implemented them (e.g., zope).
                            >>Does anyone have an explanation of why these large systems felt they needed
                            >>to implement interfaces?[/color]
                            >
                            >[/color]
                            This might help:



                            --
                            bruno desthuilliers
                            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                            p in 'onurb@xiludom. gro'.split('@')])"

                            Comment

                            Working...