473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Mar 6 '07 #1
7 2404
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

Mar 6 '07 #2
'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

Mar 6 '07 #3
p_******@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.

http://wiki.zope.org/zope3/FrontPage
http://wiki.zope.org/zope3/programmers_tutorial.pdf

-Larry
Mar 6 '07 #4
On 5 Mar 2007 16:25:03 -0800, p_******@yahoo.com <p_******@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
Mar 6 '07 #5
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(object):
def someMethod(self, argument):
raise NotImplementedError()

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.

Mar 6 '07 #6
On Mar 6, 11:55 am, "Goldfish" <gregt...@mindspring.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(object):
def someMethod(self, argument):
raise NotImplementedError()

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;
pISampleInterface = CoCreateInstance(..);
................
pISampleInterface ->QueryInstance(IID_IPoint ,(void*&)pIPoint )

// 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",dX ,dY,dZ);

10.0 20.0 30.0
Thanks
PSB

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

Mar 7 '07 #7
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

Mar 7 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred...
0
by: K_Lee | last post by:
While trying to understand some issues to python's implementation of Socket, I recorded an outline to Python's socket object interface to the native implementation in a webblog type documents in...
0
by: Phillip J. Eby | last post by:
PEP: 333 Title: Python Web Server Gateway Interface v1.0 Version: $Revision: 1.1 $ Last-Modified: $Date: 2004/08/27 17:30:09 $ Author: Phillip J. Eby <pje at telecommunity.com> Discussions-To:...
9
by: Pierre Barbier de Reuille | last post by:
Ok, I first want to stress that I looked through the newsgroups archives (on Google) for an answer to my question, but I didn't find it. It's about the interface of the set classes as defined in...
5
by: Steve | last post by:
Is it possible to design interfaces that classes must implement in Python? If it's not, is this functionality planned at all for the future? Thanks, Steve
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.