Interface Implementation in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • p_shakil@yahoo.com

    #1

    Interface Implementation in Python

    Hi,

    I would like to know the interface concept in Python.How the
    Interface is defined and implemented in Python?.

    How to access the interface fromn Client?

    Thanks
    PSB

  • MonkeeSage

    #2
    Re: Interface Implementation in Python

    On Mar 5, 6:25 pm, p_sha...@yahoo. com wrote:
    Hi,
    >
    I would like to know the interface concept in Python.How the
    Interface is defined and implemented in Python?.
    >
    How to access the interface fromn Client?
    >
    Thanks
    PSB
    Not sure exactly what you mean, but in python (like most dynamic
    languages) an "interface" is simply a behavioral type system. Any
    object can implement an interface, based on its signature. For
    example; let's say you have a file object which implements read() and
    write(). Any other object with a sufficiently similar signature (e.g.,
    StringIO), can be said to implement the same interface. An interface
    in python is simply a specified behavior, and any object which
    implements that behavior can be said to have the same "interface" .

    HTH,
    Jordan

    Comment

    • Johannes Woolard

      #3
      Re: Interface Implementation in Python

      'Lo, I think I know what you're asking:
      I would like to know the interface concept in Python.How the
      Interface is defined and implemented in Python?.
      I assume you're talking about the what Java calls an interface - and
      the simple answer is that we don't have an equivalent in Python.
      This is because in the Python system of classes, interface inheritance
      would be unenforceable (because methods can be arbitrarily added at
      runtime).
      How to access the interface fromn Client?
      I'm not completely sure what you mean here... client class, client of
      some protocol? Assuming you mean the client class, my answer above
      should suffice (and if not then I am completely missing the question).

      Regards,

      Johannes Woolard

      Comment

      • Larry Bates

        #4
        Re: Interface Implementation in Python

        p_shakil@yahoo. com wrote:
        Hi,
        >
        I would like to know the interface concept in Python.How the
        Interface is defined and implemented in Python?.
        >
        How to access the interface fromn Client?
        >
        Thanks
        PSB
        >
        You might want to look at how Zope 3 implements interfaces.




        -Larry

        Comment

        • =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

          #5
          Re: Interface Implementation in Python

          On 5 Mar 2007 16:25:03 -0800, p_shakil@yahoo. com <p_shakil@yahoo .comwrote:
          Hi,
          >
          I would like to know the interface concept in Python.How the
          Interface is defined and implemented in Python?.
          >
          How to access the interface fromn Client?
          You have a class with methods and data. You write many unit tests for
          that class which defines the behaviour of your interface. Make sure
          your class passes all those tests. When you are done, not only does
          your unit tests specify an interface, you also have a concrete class
          that implements that interface.

          Now replace the original class with another class. If that class also
          passes all your tests, then you can conclude that it also implements
          the same interface.

          --
          mvh Björn

          Comment

          • Goldfish

            #6
            Re: Interface Implementation in Python

            I would like to know the interface concept in Python.How the
            Interface is defined and implemented in Python?.
            One way I have implemented interfaces, is as follows:

            class MyInterface(obj ect):
            def someMethod(self , argument):
            raise NotImplementedE rror()

            If anybody ever uses that class directly, or subclasses it without
            implementing a real version of that method, a runtime error can be
            expected. This bridges both interfaces and abstract classes.

            As others have pointed out, this isn't quite like a Java interface,
            but I have found it useful to state my intentions up front, and that
            is how I do it. The only way to truly enforce this is by following up
            with lots of good test cases, run often.

            Comment

            • p_shakil@yahoo.com

              #7
              Re: Interface Implementation in Python

              On Mar 6, 11:55 am, "Goldfish" <gregt...@minds pring.comwrote:
              I would like to know the interface concept in Python.How the
              Interface is defined and implemented in Python?.
              >
              One way I have implemented interfaces, is as follows:
              >
              class MyInterface(obj ect):
              def someMethod(self , argument):
              raise NotImplementedE rror()
              >
              If anybody ever uses that class directly, or subclasses it without
              implementing a real version of that method, a runtime error can be
              expected. This bridges both interfaces and abstract classes.
              >
              As others have pointed out, this isn't quite like a Java interface,
              but I have found it useful to state my intentions up front, and that
              is how I do it. The only way to truly enforce this is by following up
              with lots of good test cases, run often.

              I have a class by name Point3D in Python.

              class Point3D :
              def __int(self):
              self.__x =0.0
              self.__y =0.0
              self.__z =0.0

              def setPoint(x,y,z) :
              self.__x =x
              self.__y =y
              self.__z =z

              def getPoint(x,y,z) :
              x = self.__x
              y = self.__y
              z = self.__z

              I dont want to expose the above Point3D implementation to the user /
              client side.To achieve that we can use interface concept.In Python to
              use interface concept.

              How the interface class looks like for Point3D in python?.

              How to implement that Interface?

              How to access the implemented interface thru Interface Object in the
              client side?

              It will be helpful if somebody proivide a piece of sample code for
              creating the interface for the above access at the client place

              in C++ these can be done in this way

              class IPoint : public IUnknown{
              virtual void setPoint(int x,int y,int z)=0;
              virtual void getPoint(&x,&y, &z)=0;
              }

              class Point3D: public IPoint {
              private :
              float _x ,_y,_z;
              public:
              void Point3D() {
              _x=_y=_z =0
              }
              void setPoint(int x,int y,int z){

              _x =x
              _y =y
              _z =z


              }
              void getPoint(&x,&y, &z){
              x = _x
              y = _y
              z = _z
              }
              }

              Access the Point data in the client side

              IPoint *pIPoint = NULL;
              pISampleInterfa ce = CoCreateInstanc e(..);
              ............... .
              pISampleInterfa ce ->QueryInstance( IID_IPoint ,(void*&)pIPoin t )

              // We have the pIPoint interafce
              float dX =0,dY=0,dZ=0;

              pIPoint->setPoint(10,20 ,30);

              pIPoint->getPoint(&dX ,&dY,&dZ);

              printf ( "%f,%f%f",d X ,dY,dZ);

              10.0 20.0 30.0


              Thanks
              PSB

              --------------------------------------------------------------------------------



              Comment

              • MonkeeSage

                #8
                Re: Interface Implementation in Python

                On Mar 6, 6:23 pm, p_sha...@yahoo. com wrote:
                I dont want to expose the above Point3D implementation to the user /
                client side.To achieve that we can use interface concept.In Python to
                use interface concept.
                In python you would use name mangling to hide parts of the interface
                from the public.

                class Point3D:
                def __init__(self):
                self.__x = 0.0
                self.__y = 0.0
                self.__z = 0.0

                def __setPoint(x,y, z):
                self.__x = x
                self.__y = y
                self.__z = z

                def __getPoint(x,y, z):
                x = self.__x
                y = self.__y
                z = self.__z

                p = Point3D()
                p.__setPoint(1, 2,3)

                # ...
                # AttributeError: Point3D instance has no attribute '__setPoint'

                Regards,
                Jordan

                Comment

                Working...