Am I missing something with Python not having interfaces?

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

    Am I missing something with Python not having interfaces?

    Studying OOP and noticed that Python does not have Interfaces. Is
    that correct? Is my schooling for nought on these OOP concepts if I
    use Python. Am I losing something if I don't use the "typical" oop
    constructs found in other languages (Java, C# come to mind.) I'm
    afraid that if I never use them I'll lose them and when I need them
    for something beside Python, I'll be lost. Thank you.
  • Mike Driscoll

    #2
    Re: Am I missing something with Python not having interfaces?

    On May 6, 8:44 am, jmDesktop <needin4mat...@ gmail.comwrote:
    Studying OOP and noticed that Python does not have Interfaces.  Is
    that correct?  Is my schooling for nought on these OOP concepts if I
    use Python.  Am I losing something if I don't use the "typical" oop
    constructs found in other languages (Java, C# come to mind.)  I'm
    afraid that if I never use them I'll lose them and when I need them
    for something beside Python, I'll be lost.  Thank you.
    In my school, we didn't even discuss the concept of interfaces (except
    for CLI and GUI, that is). So I looked it up. I assume you are
    referring to something like what's found here:



    If so, then it looks like an Interface is a generic class with method
    stubs. You can do that with Python just as easily by creating empty
    methods with just the "pass" keyword. It also reminds me of
    Decorators...so you might want to look at those.

    Since it's just a construct to implement polymorphism, I don't think
    you'll lose anything. However, Python does not require you to re-
    implement every method of the class it is inheriting from. You can
    just override those that you want and leave the others alone.

    Hopefully I understand this correctly...oth erwise, just ignore my
    babbling and hand waving.

    Mike

    Comment

    • hdante

      #3
      Re: Am I missing something with Python not having interfaces?

      On May 6, 10:44 am, jmDesktop <needin4mat...@ gmail.comwrote:
      Studying OOP and noticed that Python does not have Interfaces.  Is
      that correct?  Is my schooling for nought on these OOP concepts if I
      use Python.  Am I losing something if I don't use the "typical" oop
      constructs found in other languages (Java, C# come to mind.)  I'm
      afraid that if I never use them I'll lose them and when I need them
      for something beside Python, I'll be lost.  Thank you.
      Python supports interfaces. In the example below, "Vehicle" is an
      interface.

      class Vehicle:
      def drive(self, count): raise Exception("I'm only an
      interface... :-(")
      def number_of_wheel s(self): return 0
      def fly(self): pass

      class Car(Vehicle):
      def drive(self, count): print "The car walked %d steps" % count
      def number_of_wheel s(self): return 4

      As you can see, there are a couple of ways you can tell others
      "Vehicle" is an interface, like raising exceptions, returning useless
      values or doing nothing. You could also raise an exception in
      Vehicle.__init_ _.

      Comment

      • A.T.Hofkamp

        #4
        Re: Am I missing something with Python not having interfaces?

        On 2008-05-06, jmDesktop <needin4mation@ gmail.comwrote:
        Studying OOP and noticed that Python does not have Interfaces. Is
        that correct? Is my schooling for nought on these OOP concepts if I
        Depends on your definition of 'Python does not have Interfaces'. They are not
        in the official language, but they do exist. look into zope.interfaces , at
        http://www.zope.org/Products/ZopeInterface .
        use Python. Am I losing something if I don't use the "typical" oop
        constructs found in other languages (Java, C# come to mind.) I'm
        I think you are still thinking with a Java mind-set (no idea about C#, never
        programmed with it).

        Interfaces mainly exist to formalize TO A COMPILER that an object will provide
        certain stuff. In this way, the compiler can catch such errors at compile time.

        Python on the other hand does very little at compile time (other than parse
        errors). Instead, at run-time it performs the checks that something you want to
        use is actually there.
        (in the same way that you don't declare variables a priori. You simply use them
        and Python creates them for you when needed).

        As a result, you get much more light-weight, more dynamic, code, which supports
        the RAD nature of Python what makes it so much more productive.

        (after a few years Python, you may find the Java way of doing things clunky).

        afraid that if I never use them I'll lose them and when I need them
        for something beside Python, I'll be lost. Thank you.
        You can continue doing everything exactly in the way you do now. Effectively
        you would then be programming Java/C# in Python syntax. I believe you would
        gain very little by that move.

        If you dare let go of your old habits, and embrace the mindset of a new
        language, you can learn a lot more, namely that programming can be done in many
        different ways (even if all those ways are called OOP).
        If you only have a hammer, the whole world looks like a nail. If you have a
        whole toolbox, problems become much more diverse (subtle). You learn to use
        the right tools for the right problem, and maybe you find new (better) ways of
        approaching old problems.


        In the process, you may lose details of how something was done in language X,
        but that's why they have invented books.

        Sincerely,
        Albert

        Comment

        • Jim Washington

          #5
          Re: Am I missing something with Python not having interfaces?

          jmDesktop wrote:
          Studying OOP and noticed that Python does not have Interfaces. Is
          that correct? Is my schooling for nought on these OOP concepts if I
          use Python. Am I losing something if I don't use the "typical" oop
          constructs found in other languages (Java, C# come to mind.) I'm
          afraid that if I never use them I'll lose them and when I need them
          for something beside Python, I'll be lost. Thank you.
          --
          http://mail.python.org/mailman/listinfo/python-list
          Others have commented that interfaces are not normally important in Python.

          However, if you want to do something *with* interfaces like declaring
          that an object or class implements a particular interface, or querying
          the interfaces implemented by an object, you might look at
          zope.interface. Although it is used by and created for Zope,
          zope.interface is packaged to be installable and usable outside of zope.
          It's available at the Cheese Shop.

          If you want to do adaptation from one interface to another or, for
          example, register and look-up utilities by interface, zope.component is
          also handy.

          - Jim Washington

          Comment

          • jmDesktop

            #6
            Re: Am I missing something with Python not having interfaces?

            On May 6, 10:26 am, "A.T.Hofkam p" <h...@se-162.se.wtb.tue. nlwrote:
            On 2008-05-06, jmDesktop <needin4mat...@ gmail.comwrote:
            >
            Studying OOP and noticed that Python does not have Interfaces.  Is
            that correct?  Is my schooling for nought on these OOP concepts if I
            >
            Depends on your definition of 'Python does not have Interfaces'. They are not
            in the official language, but they do exist. look into zope.interfaces , athttp://www.zope.org/Products/ZopeInterface.
            >
            use Python.  Am I losing something if I don't use the "typical" oop
            constructs found in other languages (Java, C# come to mind.)  I'm
            >
            I think you are still thinking with a Java mind-set (no idea about C#, never
            programmed with it).
            >
            Interfaces mainly exist to formalize TO A COMPILER that an object will provide
            certain stuff. In this way, the compiler can catch such errors at compile time.
            >
            Python on the other hand does very little at compile time (other than parse
            errors). Instead, at run-time it performs the checks that something you want to
            use is actually there.
            (in the same way that you don't declare variables a priori. You simply usethem
            and Python creates them for you when needed).
            >
            As a result, you get much more light-weight, more dynamic, code, which supports
            the RAD nature of Python what makes it so much more productive.
            >
            (after a few years Python, you may find the Java way of doing things clunky).
            >
            afraid that if I never use them I'll lose them and when I need them
            for something beside Python, I'll be lost.  Thank you.
            >
            You can continue doing everything exactly in the way you do now. Effectively
            you would then be programming Java/C# in Python syntax. I believe you would
            gain very little by that move.
            >
            If you dare let go of your old habits, and embrace the mindset of a new
            language, you can learn a lot more, namely that programming can be done inmany
            different ways (even if all those ways are called OOP).
            If you only have a hammer, the whole world looks like a nail. If you have a
            whole toolbox, problems become much more diverse (subtle). You learn to use
            the right tools for the right problem, and maybe you find new (better) ways of
            approaching old problems.
            >
            In the process, you may lose details of how something was done in languageX,
            but that's why they have invented books.
            >
            Sincerely,
            Albert
            I would imagine this is why I haven't found any schools teaching
            Python in their basic programming classes too. On the dynamic typing,
            isn't that the same sort of thing that lots of scripting languages
            do? VBScript doesn't require you to define your variables, but I
            don't really want to use it for anything (used to use it a lot in
            Classic ASP.) I believe everyone that Python is great, but some of it
            doesn't make sense to me as to why. Thanks.

            Comment

            • cokofreedom@gmail.com

              #7
              Re: Am I missing something with Python not having interfaces?

              I would imagine this is why I haven't found any schools teaching
              Python in their basic programming classes too. On the dynamic typing,
              isn't that the same sort of thing that lots of scripting languages
              do? VBScript doesn't require you to define your variables, but I
              don't really want to use it for anything (used to use it a lot in
              Classic ASP.) I believe everyone that Python is great, but some of it
              doesn't make sense to me as to why. Thanks.
              Well, school-wise we got taught VB because it was easy for the non-
              programmers. At university I learnt Java, C++, Haskell, Fortran and
              Python. My favourite is obviously Python.

              So here is my terrible (likely wrong) view of Python:

              Python is a dynamic programming language, different to static ones
              like Java or C++, and variables-wise, well you do have types, but it
              is up to the interpreter to understand what to do with them. From my
              understanding "variables" as you call them can be the following;
              Integer 1, String "Hello", Tuple (), List [], Dictionary {} and a
              higher level function (you can assign functions as varaible names,
              that personally is amazing!).

              Python is built to be easy to read, I think Guido (BDFL) said
              something about 10% of the time code is written, 90% it is read, or
              something to that affect. The point is that anyone who knows Python to
              even a small degree should be able to pick up someone elses code and
              given a small time understand the idea of it, even with any comments.

              That is very powerful and key to the idea of Python, and my main love
              for it. The issue I get from University is this idea that OOP is the
              way to program, it is one of the ways, not the be all and end all. I
              almost died when I got shown Haskell because nothing made sense, but
              after using Python and returning, I understood more of the logic
              because I had used the same techniques (in a more readable format) in
              Python before.

              A common bug people suffer from when trying a new language is doing
              things how they would in their previous language of choice. Python is
              not Java, or C++ or anything else other than Python. So read a few
              tutorials on Python, see what people say it is good at and use it for
              that purpose. I think it is an advantage to know how to do things
              with different languages in different formats, but it is important to
              know when to use one method over another. Personally DiveIntoPython
              was a great guide for me as to the uses and benefits of Python, at a
              basic level. Try googling it and reading the (free) articles and
              tutorials online.

              And welcome to Python, the grass is greener :)

              Comment

              • Arnaud Delobelle

                #8
                Re: Am I missing something with Python not having interfaces?

                jmDesktop <needin4mation@ gmail.comwrites :
                Studying OOP and noticed that Python does not have Interfaces. Is
                that correct? Is my schooling for nought on these OOP concepts if I
                use Python. Am I losing something if I don't use the "typical" oop
                constructs found in other languages (Java, C# come to mind.) I'm
                afraid that if I never use them I'll lose them and when I need them
                for something beside Python, I'll be lost. Thank you.
                You're not missing anything. An Interface is the Poor Man's Multiple
                Inheritance. But in truth, even without multiple inheritance, Python
                wouldn't need java-like interfaces.

                --
                Arnaud

                Comment

                • Arnaud Delobelle

                  #9
                  Re: Am I missing something with Python not having interfaces?

                  cokofreedom@gma il.com writes:
                  [...]
                  Python is built to be easy to read,
                  And also very easy to *write*. I rarely hear this, but it is the main
                  reason why I like Python so much. I can't really explain why though.

                  [...]

                  (cokofreedom, I found your explanation of the virtues of Python was
                  excellent!)

                  --
                  Arnaud

                  Comment

                  • Torsten Bronger

                    #10
                    Re: Am I missing something with Python not having interfaces?

                    Hallöchen!

                    jmDesktop writes:
                    Studying OOP and noticed that Python does not have Interfaces. Is
                    that correct? Is my schooling for nought on these OOP concepts if I
                    use Python. Am I losing something if I don't use the "typical" oop
                    constructs found in other languages (Java, C# come to mind.) I'm
                    afraid that if I never use them I'll lose them and when I need them
                    for something beside Python, I'll be lost. Thank you.
                    See also http://www.python.org/dev/peps/pep-3119/

                    Tschö,
                    Torsten.

                    --
                    Torsten Bronger, aquisgrana, europa vetus
                    Jabber ID: bronger@jabber. org
                    (See http://ime.webhop.org for further contact info.)

                    Comment

                    • A.T.Hofkamp

                      #11
                      Re: Am I missing something with Python not having interfaces?

                      On 2008-05-06, jmDesktop <needin4mation@ gmail.comwrote:
                      >
                      I would imagine this is why I haven't found any schools teaching
                      Python in their basic programming classes too. On the dynamic typing,
                      I don't understand your reasoning. What part does 'this' refer to?
                      Also, you are wrong.
                      We teach 2nd year Bachelor students about programming (the process of solving a
                      problem by making a computer program, which is not the same as coding) with
                      Python.
                      isn't that the same sort of thing that lots of scripting languages
                      do? VBScript doesn't require you to define your variables, but I
                      don't really want to use it for anything (used to use it a lot in
                      VB was not designed well in that case.

                      I believe that VB is popular becomes it comes from the same firm that also
                      sells the OS and the word processor of all school computers. Also, many
                      teachers make a choice based on what others do or what they know,
                      rather than what the best possible prog language is for students.

                      Also, industry wants to have graduates that know the language they use.
                      It is very easy to give in to that desire, although it is not very useful,
                      since industry changes its programming language every so many years.

                      Classic ASP.) I believe everyone that Python is great, but some of it
                      doesn't make sense to me as to why. Thanks.
                      That's normal (and healthy, probably).

                      The thing that should really scare you however is your idea that everything
                      done in the Java/C# way does make sense (ie you are trying to push Python in
                      the 'yet another hammer' concept rather than treating it as a shiny newly
                      invented tool used by many others already).

                      Unlike what you think, all languages including Java and C# have their
                      weaknesses. However, you can only see weak points from outside the Java/C#
                      world (by knowing other languages that handle the same problems in a better
                      way).


                      (sorry if I offended you with the last bit, I did not intend to attack you
                      personally, just trying to provoke you enough to make you wonder about your
                      implicit assumptions.)

                      Sincerely,
                      Albert

                      Comment

                      • hdante

                        #12
                        Re: Am I missing something with Python not having interfaces?

                        On May 6, 12:09 pm, jmDesktop <needin4mat...@ gmail.comwrote:
                        On May 6, 10:26 am, "A.T.Hofkam p" <h...@se-162.se.wtb.tue. nlwrote:
                        >
                        >
                        >
                        On 2008-05-06, jmDesktop <needin4mat...@ gmail.comwrote:
                        >
                        Studying OOP and noticed that Python does not have Interfaces.  Is
                        that correct?  Is my schooling for nought on these OOP concepts if I
                        >
                        Depends on your definition of 'Python does not have Interfaces'. They are not
                        in the official language, but they do exist. look into zope.interfaces , athttp://www.zope.org/Products/ZopeInterface.
                        >
                        use Python.  Am I losing something if I don't use the "typical" oop
                        constructs found in other languages (Java, C# come to mind.)  I'm
                        >
                        I think you are still thinking with a Java mind-set (no idea about C#, never
                        programmed with it).
                        >
                        Interfaces mainly exist to formalize TO A COMPILER that an object will provide
                        certain stuff. In this way, the compiler can catch such errors at compile time.
                        >
                        Python on the other hand does very little at compile time (other than parse
                        errors). Instead, at run-time it performs the checks that something you want to
                        use is actually there.
                        (in the same way that you don't declare variables a priori. You simply use them
                        and Python creates them for you when needed).
                        >
                        As a result, you get much more light-weight, more dynamic, code, which supports
                        the RAD nature of Python what makes it so much more productive.
                        >
                        (after a few years Python, you may find the Java way of doing things clunky).
                        >
                        afraid that if I never use them I'll lose them and when I need them
                        for something beside Python, I'll be lost.  Thank you.
                        >
                        You can continue doing everything exactly in the way you do now. Effectively
                        you would then be programming Java/C# in Python syntax. I believe you would
                        gain very little by that move.
                        >
                        If you dare let go of your old habits, and embrace the mindset of a new
                        language, you can learn a lot more, namely that programming can be done in many
                        different ways (even if all those ways are called OOP).
                        If you only have a hammer, the whole world looks like a nail. If you have a
                        whole toolbox, problems become much more diverse (subtle). You learn to use
                        the right tools for the right problem, and maybe you find new (better) ways of
                        approaching old problems.
                        >
                        In the process, you may lose details of how something was done in language X,
                        but that's why they have invented books.
                        >
                        Sincerely,
                        Albert
                        >
                        I would imagine this is why I haven't found any schools teaching
                        Python in their basic programming classes too.  On the dynamic typing,
                        No. When teaching problem solving in the basic programming courses,
                        the language is irrelevant. You don't even need a computer for that.
                        isn't that the same sort of thing that lots of scripting languages
                        do?  VBScript doesn't require you to define your variables, but I
                        don't really want to use it for anything (used to use it a lot in
                        Classic ASP.)  I believe everyone that Python is great, but some of it
                        doesn't make sense to me as to why.  Thanks.
                        If you want to understand the design decisions behind python, read
                        its history:


                        Also, do you have an example of some problem that requires an
                        interface that you would like to know how it's done in python (or
                        maybe an explanation of why it's not possible) ?

                        Comment

                        • Matt Nordhoff

                          #13
                          Re: Am I missing something with Python not having interfaces?

                          Mike Driscoll wrote:
                          On May 6, 8:44 am, jmDesktop <needin4mat...@ gmail.comwrote:
                          >Studying OOP and noticed that Python does not have Interfaces. Is
                          >that correct? Is my schooling for nought on these OOP concepts if I
                          >use Python. Am I losing something if I don't use the "typical" oop
                          >constructs found in other languages (Java, C# come to mind.) I'm
                          >afraid that if I never use them I'll lose them and when I need them
                          >for something beside Python, I'll be lost. Thank you.
                          >
                          In my school, we didn't even discuss the concept of interfaces (except
                          for CLI and GUI, that is). So I looked it up. I assume you are
                          referring to something like what's found here:
                          >

                          >
                          If so, then it looks like an Interface is a generic class with method
                          stubs. You can do that with Python just as easily by creating empty
                          methods with just the "pass" keyword. It also reminds me of
                          Decorators...so you might want to look at those.
                          Instead of just using "pass", you should "raise NotImplementedE rror".
                          >From the docs: "In user defined base classes, abstract methods should
                          raise this exception when they require derived classes to override the
                          method."
                          Since it's just a construct to implement polymorphism, I don't think
                          you'll lose anything. However, Python does not require you to re-
                          implement every method of the class it is inheriting from. You can
                          just override those that you want and leave the others alone.
                          >
                          Hopefully I understand this correctly...oth erwise, just ignore my
                          babbling and hand waving.
                          >
                          Mike
                          --

                          Comment

                          • Max M

                            #14
                            Re: Am I missing something with Python not having interfaces?

                            Arnaud Delobelle skrev:
                            jmDesktop <needin4mation@ gmail.comwrites :
                            >
                            >Studying OOP and noticed that Python does not have Interfaces. Is
                            >that correct? Is my schooling for nought on these OOP concepts if I
                            >use Python. Am I losing something if I don't use the "typical" oop
                            >constructs found in other languages (Java, C# come to mind.) I'm
                            >afraid that if I never use them I'll lose them and when I need them
                            >for something beside Python, I'll be lost. Thank you.
                            >
                            You're not missing anything. An Interface is the Poor Man's Multiple
                            Inheritance. But in truth, even without multiple inheritance, Python
                            wouldn't need java-like interfaces.

                            Interfaces are a great concept for many things, and they can cover a few
                            soft spots in Python.

                            There is the possibility of using the great zope.interface library
                            http://pypi.python.org/pypi/zope.interface. Just use:

                            "easy_insta ll zope.interface"

                            And you have interfaces.



                            --

                            hilsen/regards Max M, Denmark


                            IT's Mad Science

                            Comment

                            • Larry Bates

                              #15
                              Re: Am I missing something with Python not having interfaces?

                              jmDesktop wrote:
                              Studying OOP and noticed that Python does not have Interfaces. Is
                              that correct? Is my schooling for nought on these OOP concepts if I
                              use Python. Am I losing something if I don't use the "typical" oop
                              constructs found in other languages (Java, C# come to mind.) I'm
                              afraid that if I never use them I'll lose them and when I need them
                              for something beside Python, I'll be lost. Thank you.
                              Zope has an implementation of interfaces that Zope 3 and Twisted both use if you
                              want to take a look.



                              Frankly, I've never missed them.

                              -Larry

                              Comment

                              Working...