Python 3:有些反转if语句(Python 3: some reversed if statement)
在我的教科书中,我遇到了这个例子:
lower = (lambda x, y: x if x < y else y) print(lower ('bb','aa'))到目前为止我用过
if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite]所以,这个'x if x <y else y'让我有点惊讶。 你可以指导我:在文档中哪里可以找到这种结构?
先谢谢你
In my textbook I came across this example:
lower = (lambda x, y: x if x < y else y) print(lower ('bb','aa'))So far I used
if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite]So, this 'x if x < y else y' astonished me a bit. Could you direct me: where in the documentation can I find this construction?
Thank you in advance
最满意答案
所以它们被称为“条件表达式”,您可以在python 3文档中找到它们,可以在2.5文档的这一部分中找到更清晰的解释。 我对条件赋值使用了很多,比如:
level = 1 if logging else 0So they are called "Conditional Expressions" and you can find them in the python 3 docs a longer clearer explanation can be found in this section of of the 2.5 docs. I use it a lot for conditional assignment like:
level = 1 if logging else 0更多推荐
发布评论