简体   繁体   English

在 function 结束之前退出 python 中的 function (没有返回值)的最佳方法是什么(例如检查失败)?

[英]What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)?

Let's assume an iteration in which we call a function without a return value.让我们假设一个迭代,我们调用 function 没有返回值。 The way I think my program should behave is explained in this pseudocode:这个伪代码解释了我认为我的程序应该表现的方式:

for element in some_list:
    foo(element)

def foo(element):
    do something
    if check is true:
        do more (because check was succesful)
    else:
        return None
    do much much more...

If I implement this in python, it bothers me, that the function returns a None .如果我在 python 中实现此功能,那么 function 返回None会让我感到困扰。 Is there a better way for "exiting a function, that has no return value, if a check fails in the body of the function"?有没有更好的方法来“退出没有返回值的 function,如果函数体中的检查失败”?

You could simply use你可以简单地使用

return

which does exactly the same as这与

return None

Your function will also return None if execution reaches the end of the function body without hitting a return statement.如果执行到达 function 主体的末尾而没有遇到return语句,您的 function 也将返回None Returning nothing is the same as returning None in Python.什么都不返回与在 Python 中返回None相同。

I would suggest:我会建议:

def foo(element):
    do something
    if not check: return
    do more (because check was succesful)
    do much much more...

you can use the return statement without any parameter to exit a function您可以使用不带任何参数的return语句退出 function

def foo(element):
    do something
    if check is true:
        do more (because check was succesful)
    else:
        return
    do much much more...

or raise an exception if you want to be informed of the problem或者如果您想了解问题,请提出异常

def foo(element):
    do something
    if check is true:
        do more (because check was succesful)
    else:
        raise Exception("cause of the problem")
    do much much more...
  1. return None or return can be used to exit out of a function or program, both does the same thing return Nonereturn可用于退出 function 或程序,两者都做同样的事情
  2. quit() function can be used, although use of this function is discouraged for making real world applications and should be used only in interpreter. quit() function 可以使用,尽管不鼓励使用此 function 来制作实际应用程序,并且只能在解释器中使用。
    import site
    
    def func():
        print("Hi")
        quit()
        print("Bye")
  1. exit() function can be used, similar to quit() but the use is discouraged for making real world applications. exit() function 可以使用,类似于quit()但不鼓励在实际应用中使用。
import site
    
    def func():
        print("Hi")
        exit()
        print("Bye")
  1. sys.exit([arg]) function can be used and need to import sys module for that, this function can be used for real world applications unlike the other two functions. sys.exit([arg]) function 可以使用,但需要为此import sys模块,此 function 可用于实际应用,与其他两个功能不同。
import sys 
  height = 150
  
if height < 165: # in cm 
      
    # exits the program 
    sys.exit("Height less than 165")     
else: 
    print("You ride the rollercoaster.") 
  1. os._exit(n) function can be used to exit from a process, and need to import os module for that. os._exit(n) function 可用于退出进程,需要import os模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python中any()函数的对立面是什么,例如没有任何 - What is the opposite of any() function in python, e.g. without any Python:如何创建一个函数? 例如 f(x) = ax^2 - Python: How to create a function? e.g. f(x) = ax^2 可视化 Python function 流(例如作为树或概念图) - Visualize Python function flow (e.g. as tree or concept map) 多个嵌套函数需要在 python 中处理连接(例如,使用 MySQLdb 连接到 mysql 服务器)的最佳方法是什么? - What is the best way to handle connections (e.g. to mysql server using MySQLdb) in python, needed by multiple nested functions? 在Dask DataFrame中修改(例如执行数学函数)列的最佳方法是什么? - What is the the best way to modify (e.g., perform math functions) a column in a Dask DataFrame? 如何输入一个 function 以可调用对象及其参数作为参数 - 例如 function 的数值积分? - How to typehint a function which takes a callable and its args as parameters - e.g. a numerical integration of a function? 在Python词典中引用mutable(例如,列表)作为值 - 什么是最佳实践? - References to mutables (e.g., lists) as values in Python dictionaries - what is best practice? python 中自适应文件路径的最佳方法是什么? 例如使用 pd.read_csv() - What is the best method for an adaptive file path in python? e.g. using pd.read_csv() 在 python 中切片的正确方法 - 例如使用 [1:] 或 [1:index 超出范围]? - Proper way to slice in python - e.g. use [1:] or [1:index which is out of range]? python中处理一系列功能检查的最佳/习惯用法是什么 - What is the best/idioms way in python to handle series of function check
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM