Declaring interface
In python, interface is defined using python class statements and is a subclass of interface.Interface which is the parent interface for all interfaces.
Syntax : class IMyInterface(zope.interface.Interface): # methods and attributes
Example
import zope.interface class MyInterface(zope.interface.Interface): x = zope.interface.Attribute( "foo" ) def method1( self , x): pass def method2( self ): pass print ( type (MyInterface)) print (MyInterface.__module__) print (MyInterface.__name__) # get attribute x = MyInterface[ 'x' ] print (x) print ( type (x)) |
Output :
<class zope.interface.interface.InterfaceClass> __main__ MyInterface <zope.interface.interface.Attribute object at 0x00000270A8C74358> <class 'zope.interface.interface.Attribute'>
Python-interface module
In object-oriented languages like Python, the interface is a collection of method signatures that should be provided by the implementing class. Implementing an interface is a way of writing an organized code and achieve abstraction.
The package zope.interface provides an implementation of “object interfaces” for Python. It is maintained by the Zope Toolkit project. The package exports two objects, ‘Interface’ and ‘Attribute’ directly. It also exports several helper methods. It aims to provide stricter semantics and better error messages than Python’s built-in abc module.