【问题标题】:In python, what does this ' return decorator(callback) if callback else decorator ' mean?在python中,这个'return decorator(callback) if callback else decorator'是什么意思?
【发布时间】:2012-07-18 18:00:38
【问题描述】:
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

最后一句是什么意思?

【问题讨论】:

  • 有一个已发布的语言标准和一个交互式解释器。在将基本问题发布到此处之前,您会更乐于查找基本问题。

标签: python return if-statement


【解决方案1】:
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 问题 .

【讨论】:

【解决方案2】:

最后一句基本意思

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

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 2013-09-05
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多