0
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

最后一句话是什么意思?

4

2 回答 2

5
return decorator(callback) if callback else decorator

翻译成:

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

这是Python使用三元表达式的方式。

有关Python 是否有三元条件运算符的更多信息,请参阅此 SO 问题? .

于 2012-07-19T14:15:17.077 回答
0

最后一句基本意思是

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
于 2012-07-19T14:15:57.063 回答