Missing interfaces in Python...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kent Johnson

    #16
    Re: Missing interfaces in Python...

    I V wrote:[color=blue]
    > To use interfaces in python, just what you would do in Java, except
    > don't use interfaces.[/color]

    +1 QOTW

    Comment

    • gmilas@gmail.com

      #17
      Re: Missing interfaces in Python...

      I am currently working in C# after I spent about 3 years almost only in
      python, The way I see thinks is that for me we need interfaces in C# to
      allow for flexibility in using OOP because we must only pass defined
      and known types at all times so for us to have the flexibility of
      passing either a Car or a Bus to the Mechanic when we actually only
      need their fixIt method we need to use a common type that will
      accommodate the constraint of having the fixIt method. Of course this
      could also be implemented using an abstract base class but since in C#
      as well as in Java we don't have multiple inheritance it is better to
      use an interface for flexibility purpose (for ex. sometime you have to
      subclass from MarshalByRefObj ect in order to use the object in a
      distributed system so inheritance is out of the question as a means to
      flexibility in that case).

      So for good and flexible OOP in C# someone should use interfaces
      because it is the only way to achive the flexibility needed but in
      python because of duck typing you always get the flexibility without
      you doing anything special like trying to come up with a type that
      accommodates some common ground of other types, so as someone say here
      "To use interfaces in python, just do what you would do in Java, except
      don't use interfaces."

      So to digress a little, for me the difference is that in C# you are
      given safety, documentation (read app. domain definitions) and
      track-ability (read refactoring, intelisense) but you have to code for
      flexibility while in python you are given flexibility but you have to
      code for safety and documentation and as far as for track-ability you
      are usually out of luck. Now many people agree that the safety you are
      given in C# is usually far from ideal (no app. domain logic safety
      given) so usually you have to code for that as well as in python, and
      my opinion is that the documentation provided by type definition wile
      ok, for best maintainability you have to provide comments anyway about
      their app. domain means just like you do in python, so the only think
      that is different is that the lack of track-ability in python is
      sometime annoying for me now when I'm going back to python after a
      little bit of C#, but the annoyance is even bigger when in C# I always
      have to think ahead of time how to make sure that my code if flexible
      enough to support easy changing.

      Gheorghe Milas

      Comment

      • olsongt@verizon.net

        #18
        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.
        >
        > Scott Huey[/color]

        Everyone is getting off track here.

        Java has interfaces because it doesn't support multiple inheritance.
        Python supports MI, so you don't need to use the seperate concept of an
        interface. You're right that an abstract class is the equivilent of an
        interface. Just create a ABC that raises NotImplementedE xceptions for
        each of the methods, and use that class as one of the base classes. Of
        course, like a lot of stuff in python, this won't throw an exception at
        'compile-time', only when you try to invoke a method that has no
        implemenation.

        The general wisdom is that Abstract Base Classes aren't pythonic
        though. If you want to be pythonic, just implement the methods for
        your 'interface', and (if necessary) check for their existance with
        hasattr before calling (or even just call the method and you'll get an
        attribute error anyway). This is referred to as duck-typing. If it
        looks like a duck, and quacks like a duck, then for all practical
        purposes it supports the 'duck' interface.

        Comment

        • Rene Pijlman

          #19
          Re: Missing interfaces in Python...

          olsongt@verizon .net:[color=blue]
          >If it looks like a duck, and quacks like a duck, then for all practical
          >purposes it supports the 'duck' interface.[/color]

          The problem with that of course, is that there's much more to being a duck
          than being called 'duck'.

          public interface JarFile {
          void explode();
          }
          public interface NuclearBomb {
          void explode();
          }


          --
          René Pijlman

          Comment

          • Peter Maas

            #20
            Re: Missing interfaces in Python...

            Fredrik Lundh schrieb:[color=blue]
            > Jonathan Daugherty wrote_
            >[color=green]
            >> # In Python, you would simply call the functions you need. No need to
            >> # make things that rigidly defined.
            >>
            >> Except when you need to handle exceptions when those methods don't
            >> exist. I think interfaces can definitely be useful.[/color]
            >
            > so with interfaces, missing methods will suddenly appear out of thin
            > air ?[/color]

            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.

            Peter Maas, Aachen

            Comment

            • Peter Maas

              #21
              Re: Missing interfaces in Python...

              Roy Smith schrieb:[color=blue]
              > Python is a very dynamic language. Java is a very static language.[/color]

              What is the difference between "static" and "very static"? Is Java
              more static than Fortran I? ;)

              Peter Maas, Aachen

              Comment

              • olsongt@verizon.net

                #22
                Re: Missing interfaces in Python...


                Rene Pijlman wrote:[color=blue]
                > olsongt@verizon .net:[color=green]
                > >If it looks like a duck, and quacks like a duck, then for all practical
                > >purposes it supports the 'duck' interface.[/color]
                >
                > The problem with that of course, is that there's much more to being a duck
                > than being called 'duck'.
                >
                > public interface JarFile {
                > void explode();
                > }
                > public interface NuclearBomb {
                > void explode();
                > }
                > http://www.beust.com/weblog/archives/000269.html
                >
                > --
                > René Pijlman[/color]

                Not that I disagree with you, but interfaces don't really guarantee any
                semantics either. You'd probably need to use Eiffel if you really want
                to design by contract.

                public class EarFile implements JarFile {
                void explode()
                {
                HackerTools.Wip eHardDrive();
                }

                }

                Comment

                • Roy Smith

                  #23
                  Re: Missing interfaces in Python...

                  Peter Maas <peter.maas@som ewhere.com> wrote:[color=blue]
                  > 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
                  bouncer in front of a nightclub stopping people saying, "You can't
                  come in here unless you tell me you're over 21 and aren't wearing
                  scruffy jeans". OK, I'm happy to tell you that, but if you don't
                  check to make sure it's true, you're going to have a lot of scruffy 18
                  year olds crashing your party.

                  Comment

                  • Jonathan Daugherty

                    #24
                    Re: Missing interfaces in Python...

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

                    I don't think anyone is suggesting that interfaces do (or should)
                    prove that the implemented methods actually do the right thing.

                    --
                    Jonathan Daugherty

                    Comment

                    • Alex Martelli

                      #25
                      Re: Missing interfaces in Python...

                      Roy Smith <roy@panix.co m> wrote:
                      [color=blue]
                      > Peter Maas <peter.maas@som ewhere.com> wrote:[color=green]
                      > > 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).


                      Alex

                      Comment

                      • Kay Schluehr

                        #26
                        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.
                        >
                        > Scott Huey[/color]

                        The answer is called "duck typing" or "structural typing". Any two
                        classes that implement a set of methods with pairwise equal signatures
                        can be considered as "presenting a group of methods". You do not have
                        to create a special construct and assign it to classes ( e.g. via an
                        "implements " directive ) in order to make it work. That's because you
                        are not enforced to know the type of an object at compile time. Car and
                        Bus classes may be selected from two completely different libraries
                        without any common convention but it is still possible ( though not
                        very likely without any adaption ) that they work together and show
                        sound behaviour ( you have to prove at least certain behavioural
                        properties using unit tests ).

                        "Duck typing" is also the reason why coupling of an interface with an
                        implementation is not harmfull in Python. You won't find many deep
                        class hierarchies and extensive frameworks. This has the advantage that
                        a classification you have done once at the beginning of your project in
                        the design phase is not considered to be carved in stone.
                        In Java/C#/C++ you can achieve many of the same effects of using
                        "generic" or "templates" but if you are not start coding with them from
                        the very beginning you loose many of their benfits. In Python this is a
                        non-issue.

                        Comment

                        • Rene Pijlman

                          #27
                          Re: Missing interfaces in Python...

                          Kay Schluehr:[color=blue]
                          >You won't find many deep class hierarchies and extensive frameworks.[/color]

                          Zope comes to mind.
                          [color=blue]
                          >This has the advantage that a classification you have done once at
                          >the beginning of your project in the design phase is not considered
                          >to be carved in stone.[/color]

                          Zope 3 comes to mind.

                          Comment

                          • bruno at modulix

                            #28
                            Re: Missing interfaces in Python...

                            olsongt@verizon .net wrote:
                            (snip)
                            [color=blue]
                            > Everyone is getting off track here.[/color]

                            Not that much...
                            [color=blue]
                            > Java has interfaces because it doesn't support multiple inheritance.[/color]

                            Java as interfaces because it relies on type declaration for subtyping
                            *and* doesn't support MI.
                            [color=blue]
                            > Python supports MI, so you don't need to use the seperate concept of an
                            > interface.[/color]

                            s/supports MI/doesn't rely on type declaration for subtyping/

                            Would we need interfaces in Python if Python did not support MI ? Of
                            course not, duck typing would still work.

                            (snip)

                            [color=blue]
                            > The general wisdom is that Abstract Base Classes aren't pythonic
                            > though.[/color]

                            *Pure* abstract base classes (ie: abc without any implementation) are
                            not Pythonic. I often use abc's that provides the 'guts' for common
                            stuff, but are meant to be specialized for use (this is pretty common in
                            frameworks).

                            (snip the rest - mostly agree)

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

                            Comment

                            • bruno at modulix

                              #29
                              Re: Missing interfaces in Python...

                              Rene Pijlman wrote:[color=blue]
                              > Kay Schluehr:
                              >[color=green]
                              >>You won't find many deep class hierarchies and extensive frameworks.[/color]
                              >
                              >
                              > Zope comes to mind.
                              >
                              >[color=green]
                              >>This has the advantage that a classification you have done once at
                              >>the beginning of your project in the design phase is not considered
                              >>to be carved in stone.[/color]
                              >
                              >
                              > Zope 3 comes to mind.[/color]

                              Yeps. Now Zope is a world in itself, and is not really pythonic IMHO.

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

                              Comment

                              • Ben

                                #30
                                Re: Missing interfaces in Python...


                                bruno at modulix wrote:[color=blue]
                                > Rene Pijlman wrote:[color=green]
                                > > Kay Schluehr:
                                > >[color=darkred]
                                > >>You won't find many deep class hierarchies and extensive frameworks.[/color]
                                > >
                                > >
                                > > Zope comes to mind.
                                > >
                                > >[color=darkred]
                                > >>This has the advantage that a classification you have done once at
                                > >>the beginning of your project in the design phase is not considered
                                > >>to be carved in stone.[/color]
                                > >
                                > >
                                > > Zope 3 comes to mind.[/color]
                                >
                                > Yeps. Now Zope is a world in itself, and is not really pythonic IMHO.
                                >[/color]

                                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.

                                Cheers,
                                Ben
                                [color=blue]
                                > --
                                > bruno desthuilliers
                                > python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                                > p in 'onurb@xiludom. gro'.split('@')])"[/color]

                                Comment

                                Working...