Search code examples
pythonclasspython-2.7methodsrestriction

Python method parameter restricted class


I want to restrict a parameter within a set of options. If the function is called a parameter must be restricted to a couple of options. This is what I have until now

class GetFileMethod:
     URL = 'url'
     ATTACHMENT = 'attachment'

class MailClient
     def GetFile(self,method)

MailClient.GetFile(GetFileMethod.URL) #works ok, but
MailClient.GetFile("lalala") #should raise an error

Any suggestions?


Solution

  • def GetFile(self, method):
        if  method not in {'url','attachment'}:
            raise ValueError
    

    I would make GetFileMethod a method of the MailClient class and it will make life easier controlling the input.