[英]Python Idiom for Boolean Evaluation of Assignment
I have the following C program as an example of what I would like to be able to do in python: 我有以下C程序作为我希望能够在python中执行的示例:
foo@foo:~/$ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool get_false(){
return false;
}
bool get_true(){
return true;
}
void main(int argc, char* argv[]){
bool x, y;
if ( x = get_false() ){
printf("Whiskey. Tango. Foxtrot.\n");
}
if ( y = get_true() ){
printf("Nothing to see here, keep moving.\n");
}
}
foo@foo:~/$ gcc test.c -o test
test.c: In function ‘main’:
test.c:13: warning: return type of ‘main’ is not ‘int’
foo@foo:~/$ ./test
Nothing to see here, keep moving.
foo@foo:~/$
In python, the only way I know how to do this is: 在python中,我知道如何执行此操作的唯一方法是:
foo@foo:~/$ cat test.py
def get_false():
return False
def get_true():
return True
if __name__ == '__main__':
x = get_false()
if x:
print "Whiskey. Tango. Foxtrot."
y = get_true()
if y:
print "Nothing to see here, keep moving."
#if (z = get_false()):
# print "Uncommenting this will give me a syntax error."
#if (a = get_false()) == False:
# print "This doesn't work either...also invalid syntax."
foo@foo:~/$ python test.py
Nothing to see here, keep moving.
Why? 为什么? Because I'd like to be able to say: 因为我想说:
if not (x=get_false()): x={}
Basically I'm working around a bad API where the type returned is either a dict when data is available, or False. 基本上,我正在处理一个错误的API,该API返回的类型要么是数据可用时的dict,要么是False。 Yes, a valid answer would be to return consistent types and to use Exceptions instead of False for a failure mode indicator. 是的,一个有效的答案将是返回一致的类型,并对故障模式指示符使用Exception而不是False。 I can't change the underlying API though, and I run into this pattern quite a bit in environments like Python with dynamic typing (read: without strict typing for function/method interfaces). 但是我无法更改底层API,并且在带有动态类型(如Python)的环境中(使用函数/方法接口没有严格类型的输入)遇到了很多这种模式。
Any suggestions on how to reduce the if/else overhead? 关于如何减少if / else开销的任何建议?
You can use 您可以使用
x = get_false() or {}
Should get_false()
return a False
value, Python will return the second operand of or
. 如果get_false()
返回False
值,Python将返回or
的第二个操作数。
See section 5.10 of the Python reference manual. 请参阅Python参考手册的5.10节 。 (It's been there since at least Python 2.0 ). ( 至少从Python 2.0开始就已经存在)。
You are compounding an inconvenient API with repetitive bad patch up code thus compounding the problem that you seek to avoid. 您将不便的API与重复的错误修补代码混合在一起,从而使您想避免的问题更加复杂。
def wrapper():
x = get_false()
if not x:
x = dict()
return x
Then your code isn't littered with hard-to-read ternary (or ternary-like) operations and you can change wrapper to raise exceptions if you find that more suitable. 这样,您的代码就不会被难以理解的三元(或类似三元)操作所困扰,并且如果发现更合适的话,可以更改包装器以引发异常。
What you can't do is have an assignment as a conditional as in C; 您不能做的就是像C中那样有条件地赋值。 Python doesn't do that. Python不会那样做。
You could use the Python ternary operator for this: 您可以为此使用Python 三元运算符 :
>>> data=False # could be data=readyourapi()
>>> x=data if data else {}
>>> x
{}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.