Must the statement after return in the python function not execute?
- 2020-06-07 04:47:57
- OfStack
preface
The return statement is used to exit a function and return an expression to the caller. return returns None by default without arguments (or without return statements written). None is a special value whose data type is NoneType. NoneType is a special type of Python, which has only one value None.
It doesn't support any operations or built-in methods, always returns false when comparing it to any other data type, and can assign None to any variable...
1. When return is not explicit, None is returned by default
>>> def fun():
print 'ok'
>>> res=fun()
ok
>>> type(res)
<type 'NoneType'>
>>> res==None
True
>>> def func():
print 98
return
>>> f=func()
98
>>> f
>>> type(f)
<type 'NoneType'>
>>> f==None
True
2 always returns false when comparing equality with any other data type
>>> 'python'==None
False
>>> ''==None
False
>>> 9==None
False
>>> 0.0==None
False
3) When the return statement is executed, the function will exit and the statement after return will no longer be executed. An exception is to place the return statement in the try block.
def fun():
print 98
return 'ok'# Perform to the return Statement, the function terminates and subsequent statements are no longer executed
print 98
def func():
try:
print 98
return 'ok' # I get the function 1 A return value
finally:#finally The statements in the statement block are still executed
print 98
print fun()
print '----------'
print func()
Operation results:
98
ok
----------
98
98
ok
conclusion