Home  >  Article  >  Backend Development  >  Are the statements after return in Python functions executed?

Are the statements after return in Python functions executed?

零下一度
零下一度Original
2017-07-09 12:01:143759browse

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.

It does not support any operations and does not have any built-in methods. It will always return false when compared with any other data type for equality, and None can be assigned to any variable. . .


1》When the function does not explicitly return, the default value is None


>>> def fun(): 
 print 'ok' 
 
>>> res=fun() 
ok 
>>> type(res) 
<type &#39;NoneType&#39;> 
>>> res==None 
True 
>>> def func(): 
 print 98 
 return 
 
>>> f=func() 
98 
>>> f 
>>> type(f) 
<type &#39;NoneType&#39;> 
>>> f==None 
True

2》Always returns false when compared with any other data type for equality


>>> &#39;python&#39;==None 
False 
>>> &#39;&#39;==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 &#39;ok&#39;#执行到该return语句时,函数终止,后边的语句不再执行 
 print 98 
 
def func(): 
 try: 
  print 98 
  return &#39;ok&#39; #函数得到了一个返回值 
 finally:#finally语句块中的语句依然会执行 
  print 98 
 
print fun() 
print &#39;----------&#39; 
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn