如何实现“EXIT_CODES”?在Python中?
最初我想做类似的事情:
#EXIT CODES
class ExitCode(object):
(USERHOME_INVALID, \
USERHOME_CANNOT_WRITE, \
USERHOME_CANNOT_READ, \
BASHRC_INVALID) = range(-1, -5, -1)
但后来我意识到我必须确切地知道 EXIT_CODES 的总数,以便我可以将其传递给 range() 函数。假设我有 87 个(任意)EXIT_CODES...我不想数到 87(并不是说这很难),但我正在寻找一个更优雅的解决方案。
有什么建议吗?
编辑: EXIT_CODE 是一个负整数,将传递给 sys.exit 。我不喜欢写数字,而是更喜欢使用某种常量(例如 C 中的#defines 或枚举,或者 Java 中的枚举)。
Initially i thought to do something like:
#EXIT CODES
class ExitCode(object):
(USERHOME_INVALID, \
USERHOME_CANNOT_WRITE, \
USERHOME_CANNOT_READ, \
BASHRC_INVALID) = range(-1, -5, -1)
But than I've realized that I'll have to know exactly the total number of EXIT_CODES, so that I can pass it to the range() function. Let's suppose I'll have 87 (arbitrary) EXIT_CODES... I don't want to count to 87 (not that it's hard) but I am looking for a more elegant solution.
Any suggestions ?
EDIT:
EXIT_CODE is a negative int that will be passed to sys.exit . Instead of writing the number I prefer to use some sort of constants (something like #defines or enums in C, or enums in Java).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来您想要的是 C# 或其他类似语言中的枚举的 Python 等效项。 如何在 Python 中表示“枚举” ? 提供了多种解决方案,但它们仍然需要您拥有的项目数量。
编辑: 我如何表示 ' Python 中的 Enum'? 看起来方式更好。
或者你可以尝试这样的事情(但可能不是最好的解决方案):
Sounds like what you want is the Python equivalent of an enumeration in C# or other similar languages. How can I represent an 'Enum' in Python? provides several solutions, though they still require the number of items you have.
EDIT: How can I represent an 'Enum' in Python? looks way better.
Or you could try something like this (probably not the best solution, though):
也许我不明白这个问题,但为什么不简单地制作一个退出代码字典并在函数中实现所需的行为呢?
所以你可以像这样使用它
如果你想“自动”分配数字(我不推荐这样做)你可以简单地创建一个退出代码列表并返回索引的负数:(
对于最后一个,你可以实现类似基于字典的功能)
Maybe I don't understand the question, but why don't you simply make a dictionary of exit codes and implement the desired behaviour in a function?
So you can use it like
And if you want to make "automatic" assignment of numbers (I don't recommend this) you can simply create a list of exit codes and return the negative of the index:
(for the last one, you can implement a function similar to the dictionary-based one)
我必须指出,根本不确定负状态对于 sys.exit(); 是否有意义。至少在 Linux 上,它将被解释为无符号 8 位值(范围 0-255)。对于枚举类型,可以执行如下操作:
导致如下结果:
普通 Unix 系统中的预定义值为 EXIT_FAILURE=1 和 EXIT_SUCCESS=0。
附录:考虑到对标识符的 IDE 识别的担忧,还可以执行以下操作:
第二次编辑:无法避免。这是一个变体,它按照您编写名称的顺序分配值。
显然,不断增长的复杂性是不优雅的,但它确实有效。
I must note that it's not at all certain a negative status makes sense for sys.exit(); at least on Linux, it will be interpreted as an unsigned 8-bit value (range 0-255). As for an enumerated type, it's possible to do something like:
Resulting in something like:
The predefined values in normal Unix systems are EXIT_FAILURE=1 and EXIT_SUCCESS=0.
Addendum: Considering the concern about IDE identification of identifiers, one could also do something like:
Second edit: Couldn't keep away. Here's a variant that assigns the values in the order you've written the names.
Obviously the growing complexity is inelegant, but it does work.
我想我之前看过这个问题但没有看到它,但显而易见的一件事就是使用字典。
I guess I looked at this question earlier and didn't see it, but one obvious thing to do is use a dict.
您可以使用 Python 即时创建变量(或类属性)。例如
You can create variables (or class attributes) on-the-fly with Python. For example