1

I have an Enum as follows:

class RequestMethodVerbMapping(Enum):
    POST = 'Create'
    PUT = 'Update'
    DELETE = 'Delete'

Now, in order to access the string associated with a certain HTTP verb, I do the following:

In [19]: RequestMethodVerbMapping.POST.value                                                                                                                                                                                                                             
Out[19]: 'Create'

Works as expected.However, now the HTTP verb is a class attribute and I want to access the enum in a class method. I did the following:

import RequestMethodVerbMapping

class BaseWorkFlow:
    def __init__(self, request_method):
        self.request_method = request_method

def print_enum(self):
    print (RequestMethodVerbMapping.self.request_method.value)

However, this does not work and gives me an error:

AttributeError: self

What am i doing wrong ?

1 Answer 1

1

getattr() to get the Enum value by the attribute name should do the job:

def print_enum(self):
    print(getattr(RequestMethodVerbMapping, self.request_method).value)
Sign up to request clarification or add additional context in comments.

2 Comments

No..request_method is not an enum..request_method is actually Django's request method which returns POST,PUT etc. I then use that to find the appropriate string using my Enum..
@Amistad gotcha, how about this version?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.