实际上,由于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. [工具类]将时间转换为unix时间戳格式

    写在前面 由于在数据库中存的时间有时间戳格式的数据,在解析以及保存的时候,就需要考虑到数据格式的兼容性问题.看到数据库中的时间字段基本上都是以时间戳格式存储的,没办法,只能将时间进行转换了,考虑到其他 ...

  2. setter方法的内存错误

    - (void)setList:(ClassicList *)list { self.list = list; _titleLabel.text = list.activityName; _addre ...

  3. 0505--鲜花售卖网之“NABCD模型”

    一.NABCD 1) N (Need 需求)--(分析人:梁植淋) --简介 我们的鲜花售卖系统主要是给需要买花或者是养花的顾客提供一个购花平台,买花时通过位置识别给用户提供附近的花店购花,花店接到订 ...

  4. 软工实践个人练习-使用github进行代码管理

    1.掌握使用Git进行代码版本,使用github进行代码托管. 2.创建小组Organization,并邀请组员进来. 3.将代码库https://github.com/sefzu2015/AutoC ...

  5. 【HDU 5105】Math Problem

    题意 f(x)=|ax3+bx2+cx+d| 求f(x)在L≤x≤R的最大值. 分析 参数有可能是0,注意分类讨论 1.当a=0时 b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间 ...

  6. 【poj1962】 Corporative Network

    http://poj.org/problem?id=1962 (题目链接) 时隔多年又一次写带权并查集. 题意 n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x ...

  7. codeforces 715B:Complete The Graph

    Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...

  8. HD1385Minimum Transport Cost(Floyd + 输出路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  9. mongodb(基础用法)

    驱动和客户端库 https://mongodb-documentation.readthedocs.org/en/latest/ecosystem/drivers.html#id2 https://m ...

  10. Matplotlib中文设置

    1.中文设置方法,代码前加入语句 from pylab import mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] 2.例子 # -*- codin ...