downmovies 发表于 2018-8-5 06:03:10

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):
页: [1]
查看完整版本: python中的@符号的作用