实际上,由于python是动态语言,支持多继承,因此接口在语言层面,是没有的东东。

然后,在架构设计上,我们又需要这么一个东西,来规范开发人员的行为。

因此就有了zope.interface的诞生。

定义接口

继承 zope.interface.Interface即可,如下:

import zope.interface
class IFoo(zope.interface.Interface):
"""Foo blah blah"""
x = zope.interface.Attribute("""x blah blah""")
def bar(q, r=None):
"""bar blah blah"""

1. 接口不是类

>>> type(IFoo)
<class 'zope.interface.interface.InterfaceClass'>
>>> class A(object):
pass >>> type(A)
<type 'type'>

但能正常访问:__dict__ / __name__ / __module__ / __doc__ 等专有属性

2. 行为有点类似于字典

>>> list(IFoo)
['x', 'bar']
>>> IFoo.get('bar')
<zope.interface.interface.Method object at 0x0000000002B7B710>
>>> callable(IFoo.get('bar'))
True

3. 接口中的方法能自动获取签名

>>> IFoo['bar'].getSignatureString()
'(q, r=None)'

实现接口

在声明接口之前,有两个术语要说明下:

provide(提供接口)

对象实例(object)提供接口,接口详细描述/规范了对象的行为。

implement(实现)

类(可以抽象成工厂类)实现(implement)接口。

类一般不会提供(provide)接口。只有对象提供接口。【备注:此处属于术语概念,了解即可】

1. 实现接口:

a). 用类实现

>>> class Foo:
zope.interface.implements(IFoo)

有点诧异:没有报错!对象实例呢?

>>> f = Foo()

也没有报错!然后下面却报错了:

>>> IFoo['bar'](1)

Traceback (most recent call last):
File "<pyshell#85>", line 1, in <module>
IFoo['bar'](1)
File "C:\Python27\lib\site-packages\zope\interface\interface.py", line 616, in __call__
raise BrokenImplementation(self.interface, self.__name__)
BrokenImplementation: An object has failed to implement interface <InterfaceClass __main__.IFoo> The bar attribute was not provided.

问题:该如何在子类的实例中使用接口里面定义的方法?

如何规范开发人员的行为?

答案是:通过测试来发现对象或者类是否实现了定义的接口。通过代码测试来做。通过测试控制开发,似乎有点马后炮。

主要使用 zope.interface.verify 里面的 verifyObject 和 verifyClass方法。可以参考这里

>>> class IFoo(Interface):
x = Attribute("The X attribute")
def bar(q,r=None):
"""bar of interface""" >>> class Foo:
implements(IFoo) >>> from zope.interface.verify import verifyObject, verifyClass
>>> verifyObject(IFoo, Foo()) Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
verifyObject(IFoo, Foo())
File "C:\Python27\lib\site-packages\zope\interface\verify.py", line 105, in verifyObject
return _verify(iface, candidate, tentative, vtype='o')
File "C:\Python27\lib\site-packages\zope\interface\verify.py", line 62, in _verify
raise BrokenImplementation(iface, name)
BrokenImplementation: An object has failed to implement interface <InterfaceClass __main__.IFoo> The x attribute was not provided. >>> verifyClass(IFoo, Foo) Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
verifyClass(IFoo, Foo)
File "C:\Python27\lib\site-packages\zope\interface\verify.py", line 102, in verifyClass
return _verify(iface, candidate, tentative, vtype='c')
File "C:\Python27\lib\site-packages\zope\interface\verify.py", line 62, in _verify
raise BrokenImplementation(iface, name)
BrokenImplementation: An object has failed to implement interface <InterfaceClass __main__.IFoo> The bar attribute was not provided.

  

b) 用实例变量实现:

>>> b= Foo()
>>> b = zope.interface.implementer(IFoo)(b)
>>> list(zope.interface.providedBy(b))
[<InterfaceClass __main__.IFoo>]

在python 2.6后,可以直接用修饰符,上面代码的等同代码如下:

>>> from zope.interface import implementer
>>> @implementer(IFoo)
class Foo:
pass >>> list(zope.interface.implementedBy(Foo))
[<InterfaceClass __main__.IFoo>]

  

2. 查看类实现了哪些接口

>>> list(zope.interface.implementedBy(Foo))
[<InterfaceClass __main__.IFoo>]

测试某接口是否由某类实现:

>>> IFoo.implementedBy(Foo)
True

3. 查看对象实例实现了哪些接口

>>> list(zope.interface.providedBy(f))
[<InterfaceClass __main__.IFoo>]

测试某接口是否由某对象提供

>>> IFoo.providedBy(f)
True

  

接口的继承

与python的类继承相似。又有所不同,接口继承不是深度优先,如下:

>>> import zope.interface
>>> class IBase(zope.interface.Interface):
def foo():
"""base foo doc""" >>> class IBase1(IBase):
pass >>> class IBase2(IBase):
def foo():
"""base2 foo doc""" >>> class ISub(IBase1, IBase2):
pass >>> ISub['foo'].__doc__
'base2 foo doc'

  

类的继承:

>>> class Base:
def foo(self):
print "base" >>> class Base1(Base):
pass >>> class Base2(Base):
def foo(self):
print "base2" >>> class Sub(Base1,Base2):
pass >>> Sub().foo()
base

  

python 中接口的实现的更多相关文章

  1. python接口自动化(九)--python中字典和json的区别(详解)

    简介 这篇文章的由来是由于上一篇发送post请求的接口时候,参数传字典(dict)和json的缘故,因为python中,json和dict非常类似,都是key-value的形式,为啥还要这么传参,在群 ...

  2. 使用sphinx自动提取python中的注释成为接口文档

    写好了代码,交付给他人使用的时候,查看代码固然可以了解各类和函数的功能细节,但接口文档能更方便的查找和说明功能.所以,一价与代码同步的接口文档是很有必要的.sphinx可以根据python中的注释,自 ...

  3. Python 中的面向接口编程

    前言 "面向接口编程"写 Java 的朋友耳朵已经可以听出干茧了吧,当然这个思想在 Java 中非常重要,甚至几乎所有的编程语言都需要,毕竟程序具有良好的扩展性.维护性谁都不能拒绝 ...

  4. python2.7高级编程 笔记二(Python中的描述符)

    Python中包含了许多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...

  5. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

  6. centos下python中添加easygui模块

    前提:python中要集成Tkinter,Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同 ...

  7. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  8. Python::re 模块 -- 在Python中使用正则表达式

    前言 这篇文章,并不是对正则表达式的介绍,而是对Python中如何结合re模块使用正则表达式的介绍.文章的侧重点是如何使用re模块在Python语言中使用正则表达式,对于Python表达式的语法和详细 ...

  9. Python中的threading

    Python中的threading RLock--重入锁 RLock在Python中的实现是对Lock的封装,具体在类中维护了一个重入次数的变量.一旦一个线程获得一个RLock,该线程再次要求获得该锁 ...

随机推荐

  1. Memcached——分布式缓存

    下载文件:https://sourceforge.net/projects/memcacheddotnet/ 将Commons.dll,ICSharpCode.SharpZipLib.dll,log4 ...

  2. “耐撕”团队2016.04.14站立会议

    1. 时间 : 19:20--19:40  共计20分钟 2. 人员 : Z   郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客 ...

  3. u11-nav02

    header:before, header:after ,.navigation:before, .navigation:after,.nav-row:before, .nav-row:after,. ...

  4. PHP ADLogin

    <?php $user = 'aaaa'; $password = 'xxxx'; $domain = 'b.a.com'; //设定域名 $port = 3268; $basedn = 'dc ...

  5. poj 3463 最短路与次短路&&统计个数

    题意:求最短路和比最短路长度多1的次短路的个数 本来想图(有)方(模)便(版)用spfa的,结果妹纸要我看看dijkstra怎么解.... 写了三遍orz Ver1.0:堆优化+邻接表,WA //不能 ...

  6. 最小圆覆盖(Smallest Enclosing Discs)

    随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...

  7. jquery------导入jquery.2.2.3.min.js

    问题: 导入jquery.2.2.3.min.js后MyEclipse会提示代码有错误 方法: 选中jquery.2.2.3.min.js->右键->选择“MyEclipse”中的“Exc ...

  8. hdu 1013 Digital Roots

    #include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...

  9. Linux下搭建svn服务器(转)

    原文地址:http://blog.163.com/longsu2010@yeah/blog/static/173612348201202114212933/ 安装步骤如下: 1.yum install ...

  10. [Winform]DataGridView列自适应宽度

    引言 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的. 方法 ...