python – 烧瓶中的自定义装饰器不工作?

我有以下代码:

import datetime
from flask.app import Flask

app = Flask(__name__)
app.config.from_object(__name__)
app.debug = True

def track_time_spent(name):
  def decorator(f):
    def wrapped(*args, **kwargs):
      start = datetime.datetime.now()
      ret = f(*args, **kwargs)
      delta = datetime.datetime.now() - start
      print name, "took", delta.total_seconds(), "seconds"
      return ret
    return wrapped
  return decorator

@app.route('/foo')
@track_time_spent('foo')
def foo():
  print "foo"
  return "foo"

@app.route('/bar')
@track_time_spent('bar')
def bar():
  print "bar"
  return "bar"

我无法让foo返回’foo’:

$curl localhost:8888/foo
bar

(flask window) 
bar
bar took 8.2e-05 seconds
127.0.0.1 - - [18/Apr/2013 19:21:31] "GET /foo HTTP/1.1" 200 -

$curl localhost:8888/bar
bar

(flask window)
bar
bar took 3.5e-05 seconds
127.0.0.1 - - [18/Apr/2013 19:21:35] "GET /bar HTTP/1.1" 200 -

这是怎么回事?我的装饰师为什么不工作?

编辑

我认为你们似乎无法看到问题.

当我在@track_time_spent之前有@ app.route时,两个方法都返回bar.这里的错误是调用localhost:8888 / foo会导致http响应和打印功能都出现bar.

解决方法:

其他答案似乎缺少,当你切换装饰器的顺序时,你得到“bar”作为“/ foo”的响应.除非您手动更新__name __,__ module__等,否则必须在此处使用@wraps. Flask使用你的方法有点像单身,并且看你的装饰器就像wrapped()方法一样,而不是你实际包装的方法.因此,您的路线将被最后一种使用装饰器的方法覆盖.

import datetime
from functools import wraps

from flask.app import Flask

app = Flask(__name__)
app.config.from_object(__name__)
app.debug = True


def track_time_spent(name):
    def decorator(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            start = datetime.datetime.now()
            ret = f(*args, **kwargs)
            delta = datetime.datetime.now() - start
            print name, "took", delta.total_seconds(), "seconds"
            return ret
        return wrapped
    return decorator


@app.route('/foo')
@track_time_spent('foo')
def foo():
    print "foo"
    return "foo"


@app.route('/bar')
@track_time_spent('bar')
def bar():
    print "bar"
    return "bar"

app.run(host='0.0.0.0', port=8888)
上一篇:装饰模式-23种设计模式


下一篇:(python)在函数中使用装饰器进行彩色打印