python中的@符号的作用

   '@'符号用作函数修饰符是python2.4新增加的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行。也就是说@A def f(): 是非法的。只可以在模块或类定义层内对函数进行修饰,不允许修修饰一个类。一个修饰符就是一个函数,它将被修饰的函数做为参数,并返回修饰后的同名函数或其它可调用的东西。

实例(1):

def spamrun(fn):
   def sayspam(*args):
       print "spam,spam,spam"
   return sayspam

@spamrun
def useful(a,b):
   print a**2+b**2
   
useful(3,4)

结果:

spam,spam,spam

 

实例(2):

def spamrun(fn):
       print "spam,spam,spam"

@spamrun
def useful(a,b):
   print a**2+b**2

结果:

spam,spam,spam

 

实例(3):

def spamrun(fn):
   def sayspam(*args):
       print "spam,spam,spam"
   return sayspam

@spamrun
def useful(a,b):
   print a**2+b**2
   
useful(3,4)

结果:

spam,spam,spam

 

实例(4):

def addspam(fn):
   def new(*args):
       print "spam,spam,spam"
       return fn(*args)
   return new

@addspam
def useful(a,b):
   print a**2+b**2
   
useful(4,3)

结果:

spam,spam,spam
25


追加

实例

def decorator(fn):
   def test(*args):
       print "My god!"*3
       return fn(*args)
   return test

@decorator
def other(a,b):
   print a**2+b**2

if __name__=="__main__":
   other(4,3)
   other(3,4)
   
结果:

My god!My god!My god!
25
My god!My god!My god!
25
注释掉//print return fn(*args)

结果是:

My god!My god!My god!
My god!My god!My god!

    要想使other函数能正常运行,必须加返回值,@decorator是一个statement,会将other函数当作参数传入来执行test方法。

自补1

大家来看下面的代码:

def masterwebHandle(cls):
    '''
    '''
    root.putChild(cls.__name__, cls())

@masterwebHandle
class stop(resource.Resource):
    '''stop service'''
    
    def render(self, request):
        '''
        '''
        for child in GlobalObject().root.childsmanager._childs.values():
            d = child.callbackChild('serverStop')
            d.addCallback(ErrorBack)
        reactor.callLater(0.5,reactor.stop)
        return "stop"

上述内容来自于Firefly框架中的irefly\master下的webapp.py。

 

自补2

 

我们知道,Python中并没有提供直接的接口支持,但是接口技术又是现代软件设计中的重要技术,借助于它可以极大地减小软件模块间的耦合度。于是,借助于zope.interface,python中也可以引入接口技术。

 

具体的内部细节在此不展开了,直接上代码:

 

#其他省略

from zope.interface import implementer #利用这些zope.interface中implementer等技术,实现了类似于其他高级语言中的极简化的接口操作

 

from twisted.internet.interfaces import IReactorFDSet

#......

 

@implementer(IReactorFDSet)
class _ContinuousPolling(posixbase._PollLikeMixin,
                 posixbase._DisconnectSelectableMixin):

    #......

    def addReader(self, reader):
        """
        Add a C{FileDescriptor} for notification of data available to read.
        """
        self._readers.add(reader)
        self._checkLoop()


    def addWriter(self, writer):
        """
        Add a C{FileDescriptor} for notification of data available to write.
        """
        self._writers.add(writer)
        self._checkLoop()

 

其中,接口IReactorFDSet的部分代码如下(接口中只是简单地声明函数,而且注意成员函数参数中没有上面的self):

 

class IReactorFDSet(Interface):
      def addReader(reader):

      def addWriter(writer):

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值