enum - getting value of enum on string conversion

后端 未结 2 1410
渐次进展
渐次进展 2020-12-01 03:01

I have following enum defined

from enum import Enum


class D(Enum):
    x = 1
    y = 2


print(D.x)

now the printed value is

<         


        
2条回答
  •  暖寄归人
    2020-12-01 04:00

    I implemented access using the following

    class D(Enum):
        x = 1
        y = 2
    
        def __str__(self):
            return '%s' % self.value
    

    now I can just do

    print(D.x) to get 1 as result.

    You can also use self.name in case you wanted to print x instead of 1.

提交回复
热议问题