title: Is it possible to override __new__ in an enum to parse strings to an instance?
body: <p>I want to parse strings into python enums. Normally one would implement a parse method to do so. A few days ago I spotted the <strong>__new__</strong> method which is capable of returning different instances based on a given parameter.</p>
  
  <p>Here my code, which will not work:</p>
  
  <pre><code>import enum
  class Types(enum.Enum):
    Unknown = 0
    Source = 1
    NetList = 2
  
    def __new__(cls, value):
      if (value == "src"):  return Types.Source
  #    elif (value == "nl"): return Types.NetList
  #    else:                 raise Exception()
  
    def __str__(self):
      if (self == Types.Unknown):     return "??"
      elif (self == Types.Source):    return "src"
      elif (self == Types.NetList):   return "nl"
  </code></pre>
  
  <p>When I execute my Python script, I get this message:</p>
  
  <pre><code>[...]
    class Types(enum.Enum):
  File "C:\Program Files\Python\Python 3.4.0\lib\enum.py", line 154, in __new__
    enum_member._value_ = member_type(*args)
  TypeError: object() takes no parameters
  </code></pre>
  
  <p>How can I return a proper instance of a enum value?</p>
  
  <h2>Edit 1:</h2>
  
  <p>This Enum is used in URI parsing, in particular for parsing the schema. So my URI would look like this</p>
  
  <pre><code>nl:PoC.common.config
  &lt;schema&gt;:&lt;namespace&gt;[.&lt;subnamespace&gt;*].entity
  </code></pre>
  
  <p>So after a simple string.split operation I would pass the first part of the URI to the enum creation.</p>
  
  <pre><code>type = Types(splitList[0])
  </code></pre>
  
  <p>type should now contain a value of the enum Types with 3 possible values (Unknown, Source, NetList)</p>
  
  <p>If I would allow aliases in the enum's member list, it won't be possible to iterate the enum's values alias free.</p>