单独返回在python中做什么? [英] What does return alone do in python?
本文介绍了单独返回在python中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def process_blog(blogid):博客 = get_blog(blogid)如果博客 == 无:返回别的:返回博客
第 4 行的 return 有什么作用?.当 blog == None 时它返回什么?
解决方案
一个空的 return
(或者在函数末尾根本没有 return
)等于返回无
:
<小时>
仅供参考,您应该使用 is None
来检查 None´ 而不是
== None`.但是,在您的情况下,整个函数可以简化为这个(这使它有点毫无意义):
def process_blog(blogid):返回 get_blog(blogid)
def process_blog(blogid):
blog = get_blog(blogid)
if blog == None:
return
else:
return blog
What does this return in the 4th line do?. What does it returns when blog == None.?
解决方案
A bare return
(or no return
at all at the end of the function) is equal to return None
:
>>> import dis
>>> def a():
... return None
...
>>> def b():
... return
...
>>> dis.dis(a)
2 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
>>> dis.dis(b)
2 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
FYI, you should use is None
to check against None´ instead of
== None`. However, in your case the whole function can be simplified to this (which makes it somewhat pointless):
def process_blog(blogid):
return get_blog(blogid)
这篇关于单独返回在python中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文