create enum using other property name's value

jacobcan118 :

how can I create enum class that its property use the value from other member? Like my following code

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'
'''
Chris Doyle :

Dont try to refer to the variables inside as an enum, just uses them like local variables.

from enum import Enum


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


print(*[f"{var=}" for var in ProjectPath], sep="\n")

Output

var=<ProjectPath.home: '~/home'>
var=<ProjectPath.app: '~/home/app'>
var=<ProjectPath.prefix: '~/home/app/prefix'>
var=<ProjectPath.postfix: '~/home/app/postfix'>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27730&siteId=1