python - 使用其他属性名称的值创建枚举

标签 python python-3.x enums python-3.7

如何创建枚举类,使其属性使用其他成员的值?就像我下面的代码

from enum import Enum
class ProjectPath(Enum):
    home = '~/home'
    app = '~/home/app'
    prefix = '~/home/app/prefix'
    postfix = '~/home/app/postfix'

'''
try to do something like
from enum import Enum
class ProjectPath(Enum):
    home = '~/home'
    app = f'{self.home.value}/app'
    prefix = f'{self.app.value}/prefix'
    postfix = f'{self.app.value}/postfix'
'''

最佳答案

只需使用:

class ProjectPath(Enum):
    home = '~/home'
    app = f'{home}/app'
    prefix = f'{app}/prefix'
    postfix = f'{app}/postfix'

关于python - 使用其他属性名称的值创建枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60565966/

相关文章:

c++ - 使用 include 定义枚举

swift - Swift 枚举中的默认参数

python - 在 OpenERP 中创建用户的问题

python - 将 pandas DataFrame 的 2 列与行条件相加

python - Homebrew - `python@3` 和 `python3` 之间有什么区别?

java - 使用 values( ) 创建枚举常量的最终 Java 类数组

python - 重命名 Pandas 中的多列

python - 更改南迁目录

python - 列表不改组 python

python - 使用对单个元素进行操作的 lambda 表达式来过滤 pandas 系列