Chinaunix

标题: python 中的 @ 是什么意思??? [打印本页]

作者: 血饮狂刀    时间: 2005-10-27 17:11
标题: python 中的 @ 是什么意思???

  1. from twisted.internet.threads import deferToThread
  2.   deferred = deferToThread.__get__

  3.   ## example code

  4.   def print_(result):
  5.     print result
  6.   
  7.   def running():
  8.       "Prints a few dots on stdout while the reactor is running."
  9.       sys.stdout.write("."); sys.stdout.flush()
  10.       reactor.callLater(.1, running)

  11. @deferred
  12.   def sleep(sec):
  13.     "A blocking function magically converted in a non-blocking one."
  14.     print 'start sleep %s' % sec
  15.     time.sleep(sec)
  16.     print '\nend sleep %s' % sec
  17.     return "ok"
  18.   
  19.   if __name__ == "__main__":
  20.      sleep(2).addBoth(print_)
  21.      reactor.callLater(.1, running)
  22.      reactor.callLater(3, reactor.stop)
  23.      reactor.run()
复制代码

就是这句话 @deferred
其中的 @ 是什么用法,那里有讲的?
作者: sakulagi    时间: 2005-10-27 20:55
标题: python 中的 @ 是什么意思???
deferred是twisted里边定义的一个decorator。decorator是Python 2.4里新引入的一个概念。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2