在python中,这个'返回装饰器(回调),如果回调其他装饰器'是什么意思?

时间:2012-07-19 14:13:10

标签: python return if-statement

def method(path,method,callback) 
    def decorator(callback):if isinstance(callback, basestring): callback = load(callback)
        for rule in makelist(path) or yieldroutes(callback):
            for verb in makelist(method):
                verb = verb.upper()
                route = Route(self, rule, verb, callback, name=name,
                              plugins=plugins, skiplist=skiplist, **config)
                self.add_route(route)
    return callback
return decorator(callback) if callback else decorator

最后一句话的含义是什么?

2 个答案:

答案 0 :(得分:5)

return decorator(callback) if callback else decorator

转换为:

if callback:
   return decorator(callback)
else:
   return decorator

Python's way表达式为ternary

有关Does Python have a ternary conditional operator?的更多信息,请参阅此SO问题

答案 1 :(得分:0)

最后一句基本上是指<​​/ p>

if callback:  # Tests to see if callback is not None in essence, although 0 and '' will also test False
    return decorator(callback)
else:    # Not needed, just for clarity sake
    return decorator