python return用法_为什么函数在python中以“return 0”而不是“return”结尾?

你能解释一下“回归0”和“回归”之间的区别吗?

例如:

do_1():

for i in xrange(5):

do_sth()

return 0

do_2():

for i in xrange(5):

do_sth()

return

上面两个函数有什么区别?

解决方法:

取决于用法:

>>> def ret_Nothing():

... return

...

>>> def ret_None():

... return None

...

>>> def ret_0():

... return 0

...

>>> ret_Nothing() == None

True

>>> ret_None() == None

True

>>> ret_0() == None

False

>>> # and...

>>> repr(ret_Nothing())

'None'

并且作为mentioned shown Tichodroma,0不等于None.但是,在布尔上下文中,它们都是False:

>>> if ret_0():

... print 'this will not be printed'

... else:

... print '0 is boolean False'

...

0 is boolean False

>>> if ret_None():

... print 'this will not be printed'

... else:

... print 'None is also boolean False'

...

None is also boolean False

标签:python,function,return

来源: https://codeday.me/bug/20190716/1480271.html

你可能感兴趣的:(python,return用法)