简体   繁体   English

python是否有EXIT_SUCCESS常量?

[英]Does python have an EXIT_SUCCESS constant?

I'm returning 0 all over the place in a python script but would prefer something more semantic, something more readable. 我在python脚本中返回0但更喜欢更具语义性,更可读的东西。 I don't like that magic number. 我不喜欢这个神奇的数字。 Is there an idea in python similar to how in C you can return EXIT_SUCCESS instead of just 0? 在python中有一个类似于C的想法,你可以返回EXIT_SUCCESS而不是0吗?

I was unable to find it here: https://docs.python.org/3.5/library/errno.html 我无法在这里找到它: https//docs.python.org/3.5/library/errno.html

I'm returning 0 我回来了0

return is not how you set your script's exit code in Python. return不是如何在Python中设置脚本的退出代码。 If you want to exit with an exit code of 0, just let your script complete normally. 如果要退出时退出代码为0,只需让脚本正常完成即可。 The exit code will automatically be set to 0. If you want to exit with a different exit code, sys.exit is the tool to use. 退出代码将自动设置为0.如果要使用其他退出代码退出, sys.exit是要使用的工具。

If you're using return values of 0 or 1 within your code to indicate whether functions succeeded or failed, this is a bad idea. 如果您在代码中使用返回值0或1来指示函数是成功还是失败,那么这是一个坏主意。 You should raise an appropriate exception if something goes wrong. 如果出现问题,您应该提出适当的例外。

Since you discuss return here, it seems like you may be programming Python like C. Most Python functions should ideally be written to raise an exception if they fail, and the calling code can determine how to handle the exceptional conditions. 因为你在这里讨论return ,看起来你可能像C一样编写Python。大多数Python函数应该被编写为在它们失败时引发异常,并且调用代码可以确定如何处理异常条件。 For validation functions it's probably best to return True or False - not as literals, usually, but as the result of some expression like s.isdigit() . 对于验证函数,最好返回TrueFalse - 通常不是文字,而是作为某些表达式的结果,如s.isdigit()

When talking about the return value of a process into its environment you caonnt use return because a module isn't a function, so a return statement at top level would be flagged as a syntax error. 在讨论进程返回其环境的值时,您可以使用return因为模块不是函数,因此顶级的return语句将被标记为语法错误。 Instead you should use sys.exit . 相反,你应该使用sys.exit

Python might seem a little minimalist in this respect, but a call to sys.exit with no arguments defaults to success ( ie return code zero). Python在这方面可能看起来有点极简,但是对没有参数的sys.exit的调用默认为成功( 返回代码为零)。 So the easiest way to simplify your program might be to stop coding it with an argument where you don't want to indicate failure! 因此,简化程序的最简单方法可能是停止使用您不希望指示失败的参数对其进行编码!

As the documentation reminds us, integer arguments are passed back, and string arguments result in a return code of 1 and the string is printed to stderr . 正如文档提醒我们的那样,传回整数参数,字符串参数返回代码为1 ,字符串打印到stderr

The language doesn't, as far as I am aware, contain any constants (while it does have features specific to some environments, if it provided exit codes their values might need to be implementation- or platform-specific and the language developers prefer to avoid this where possible. 据我所知,该语言不包含任何常量(虽然它确实具有特定于某些环境的功能,但如果它提供了退出代码,则它们的值可能需要是实现或特定于平台的,并且语言开发人员更喜欢尽可能避免这种情况。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM