3

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'
'''
1
  • 3
    just get rid of the self parts, the variable home is defined. there is no variable called self. so remove all the references to self Commented Mar 6, 2020 at 14:23

2 Answers 2

6

Just use:

class ProjectPath(Enum):
    home = '~/home'
    app = f'{home}/app'
    prefix = f'{app}/prefix'
    postfix = f'{app}/postfix'
Sign up to request clarification or add additional context in comments.

Comments

4

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'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.