Home >Backend Development >Python Tutorial >Are the statements after return in Python functions executed?
This article mainly gives you a detailed analysis and explanation of relevant information about whether the statement after the return statement in pythonfunction will definitely not be executed. In the article The introduction is very detailed and has certain reference and learning value for everyone. Friends who need it can follow the editor to learn together.
Preface
The return statement is used to exit the function and return an expression## to the caller. #. return returns None by default when no parameters are taken (or no return statement is written). None is a special value, and its data type is NoneType. NoneType is a special type of Python that has only one value: None.
1》When the function does not explicitly return, the default value is None
>>> 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 compared with any other data type for equality
>>> '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 statements after return will no longer be executed. But placing the return statement in the try statement block is an exception.
def fun(): print 98 return 'ok'#执行到该return语句时,函数终止,后边的语句不再执行 print 98 def func(): try: print 98 return 'ok' #函数得到了一个返回值 finally:#finally语句块中的语句依然会执行 print 98 print fun() print '----------' print func()
Run result:
98 ok ---------- 98 98 ok
The above is the detailed content of Are the statements after return in Python functions executed?. For more information, please follow other related articles on the PHP Chinese website!