Python 是否可以使用 if 语句编写单行 return 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18669836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to write single line return statement with if statement?
提问by user462455
Is is possible to return from a method in single line in python
是否可以从python中的单行方法返回
Looking for something like this
寻找这样的东西
return None if x is None
Tried above, and it is invalid syntax
上面试过了,语法无效
I could easily do:
我可以轻松做到:
if x is None:
return None
But just curious if I can combine above if statement into a single line
但只是好奇我是否可以将上面的 if 语句组合成一行
采纳答案by nakedfanatic
It is possible to write a standard "if" statement on a single line:
可以在一行中编写标准的“if”语句:
if x is None: return None
However the pep 8 style guiderecommends against doing this:
然而,pep 8 风格指南建议不要这样做:
Compound statements (multiple statements on the same line) are generally discouraged
通常不鼓励复合语句(同一行上的多个语句)
回答by TerryA
Yes, it's called a conditional expression:
是的,它被称为条件表达式:
return None if x is None else something_else
You need an else something
in a conditional for it to work.
您需要一个else something
in 条件才能使其工作。
回答by smac89
You could also try the list[bool]
expression:
你也可以试试这个list[bool]
表达式:
return [value, None][x == None]
Now if the second bracket evaluates to true, None is returned otherwise, the value you want to return is returned
现在,如果第二个括号评估为真,则返回 None 否则,将返回您要返回的值
回答by Steve Jessop
Disclaimer: don't actually do this. If you really want a one-liner then like nakedfanatic says just break the rule of thumb from PEP-8. However, it illustrates why return
isn't behaving as you thought it might, and what a thing would look like that does behave as you thought return
might.
免责声明:实际上不要这样做。如果你真的想要一个单线,那么就像nakedfanatic所说的那样,打破PEP-8的经验法则。然而,它说明了为什么return
行为不像你想象的那样,以及一个事物看起来像你想象的return
那样。
The reason you can't say return None if x is None
, is that return
introduces a statement, not an expression. So there's no way to parenthesise it (return None) if x is None else (pass)
, or whatever.
你不能说的原因return None if x is None
是它return
引入了一个语句,而不是一个表达式。所以没有办法把它括起来(return None) if x is None else (pass)
,或者其他什么。
That's OK, we can fix that. Let's write a function ret
that behaves like return
except that it's an expression rather than a full statement:
没关系,我们可以解决这个问题。让我们编写一个函数ret
,return
它的行为类似于一个表达式而不是一个完整的语句:
class ReturnValue(Exception):
def __init__(self, value):
Exception.__init__(self)
self.value = value
def enable_ret(func):
def decorated_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except ReturnValue as exc:
return exc.value
return decorated_func
def ret(value):
raise ReturnValue(value)
@enable_ret
def testfunc(x):
ret(None) if x is None else 0
# in a real use-case there would be more code here
# ...
return 1
print testfunc(None)
print testfunc(1)