[Python-Twisted] 源码中的interface Implement 分析

使用接口的好处多多在Gang of Four所编写的经典软件工程书籍《设计模式》  中推荐“要面向接口而不要面向实现编程”。规范的进行接口定义有助于更好的理解系统

Python是不支持接口的。

但是为什么Twisted源码中到处是Interface和 Implement呢?

原来所有的Interface和implement不是源自twisted,而是Zope。这是第二次遇到这个,第一次是plone。so dig into it

from zope.interface import implements, classImplements

From:http://www.czug.org/zope/ca/interface.rst

http://blogs.gnome.org/jamesh/2005/09/08/python-class-advisors/ 

在Twisted中经常看到这样的code:

class IProtocol(Interface):
        def .........

             ..........

对应的:

class Protocol(BaseProtocol):

    implements(interfaces.IProtocol)

    def

这是为啥捏?这让我们思考这样一个问题,一个在类中定义的一个函数竟然能改变类的行为,要理解这个首先看看Python中的metaclasses

Metaclasses

在Python中类是metaclasses的一个实例, 默认的 metaclass 是type ,当你创建一个类或者继承一个类时,相当于你建立了一个metaclasses的实例, metaclasses带有三个参数,第一个是这个类的名称,第二个是基类的tuple,第三个是attributes and methods的字典。  观察一下的两种定义方式,事实上在Python二者是等价的。

class C(object):
    a = 42

C = type('C', (object,), {'a': 42})

The metaclass for a particular class can be picked in a number of ways:

  • A __metaclass__ variable at module or class scope.
  • Use the same metaclass as the base class.

If no metaclass is specified through either of these means, an “old style” class is created. I won’t cover old style classes here.

Now in Python calling a function and creating a new instance look pretty similar. In fact the metaclass machinary doesn’t really care. The following two class definitions are also equivalent:

class C:
    __metaclass__ = type

def not_the_metaclass(name, bases, attrs):
    return type(name, bases, attrs)

class C:
    __metaclass__ = not_the_metaclass

So using a function or other callable object as the metaclass allows you to hook into the class creation without affecting the type of the resulting class.

Class Advisors

The tricks performed by the Zope implements() function are wrapped up in the zope.interface.advice module. It does so by making use of the fact that Python programs can inspect their execution stack at runtime.

  1. Walk up the stack to where the scope of the class being defined.
  2. Check to see if a “__metaclass__” variable has been set, which would indicate the that a metaclass has been specified for this particular class already.
  3. Check the module scope for a “__metaclass__” variable.
  4. Define a function advise(name, bases, cdict) that does the following:
    • Deduce the metaclass (either what __metaclass__ was set to in the class scope, the module scope, or check base classes).
    • Call the metaclass to create the new class.
    • Do something to the new class (in the case of Zope, it sets what interfaces the class implements).
  5. Set the “__metaclass__” variable in the class scope to this function.

The actual implementation is a little more complicated to handle the case of registering multiple class advisors for a single class. The actual interface provided is quite simple though:

from zope.interface.advice import addClassAdvisor

def setA():
    def advisor(cls):
        cls.a = 42
        return cls
    addClassAdvisor(advisor)

class C:
    setA()

This simply sets the attribute ‘a’ on the class after it has been created. Also, since method decorators are implemented as a single function call, they can add a class advisor as a way to perform some extra work on the class or method after the class has been constructed.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值