title: Manipulating attributes from a namedtuple() derived class
body: <p>I have a class called <code>EasyUrl()</code> that is derived from <code>urlparse.Parseresult()</code>. <code>ParseResult()</code> is instantiated when calling <code>urlparse.urlparse(url)</code>, I have a static method inside <code>EasyUrl()</code> that changes the class type of the instantiated <code>ParseResult()</code> object into a <code>EasyUrl()</code> object. I wrap the <code>urlparse.urlparse()</code> function and the class type conversion into a function <code>parse_url()</code>.</p>
  <p>The reason behind such a function, is my attempt to hack around a separate problem I don't require an answer to but would like one, is I get a <code>TypeError</code> when <code>__new__</code> is called during the instantiating process, which lets me know I have an invalid number of arguments.</p>
  <h3>Error received when instantiating <code>EasyUrl()</code> directly</h3>
  <pre><code># snippet 
  url = 'stackoverflow.com'
  url = EasyUrl(url)
  # snippet end
  
  Output:
  TypeError: __new__() takes exactly 7 arguments (2 given)
  </code></pre>
  <p>The <code>ParseResult()</code> class inherits from a <code>namedtuple()</code>.</p>
  <h3>Excerpt from urlparse library</h3>
  <pre><code>class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin):
  
      __slots__ = ()
  
      def geturl(self):
          return urlunparse(self)
  </code></pre>
  <p>Now that I have described a little functionality of the code, here is the problem. I can't access the named tuple's (ParseResult) attributes. I'm trying to implement a default scheme for <code>ParseResult()</code> if it is missing.</p>
  <p>But I can't access the attributes in the class definition.</p>
  <pre><code>import urlparse
  
  
  def parse_url(url):
      &quot;&quot;&quot; Return a parsed EasyUrl() object&quot;&quot;&quot;
      parse_result = urlparse.urlparse(url)
      return EasyUrl.EvolveParseResult(parse_result)
  
  
  class EasyUrl(urlparse.ParseResult):
  
      @staticmethod
      def EvolveParseResult(parse_result):
          &quot;&quot;&quot; Change the type of class into a EasyUrl() Object.&quot;&quot;&quot;
          parse_result.__class__ = EasyUrl
          easy_url = parse_result # For readabilty
          easy_url.init()
          return easy_url
  
      def __init__(self, url):
          self = parse_url(url) # doesn't work
  
      def init(self):
          self.url = self.geturl()
          #self.set_scheme_if_non() # Uncomment when no error is raised
  
      def set_scheme_if_non(self, scheme='http'):
          if not self.scheme:
              self.scheme = scheme
              self.url = self.geturl() # Rebuild our url with the new scheme
  
  
  
  # Passes the set_scheme_if_non trigger
  #url = 'https://stackoverflow.com'
  # Fails if statment, then attempts to set the variable,
  # but error is raised: AttributeError: can't set attribute
  url = 'stackoverflow.com'
  
  # Will give the error: TypeError: __new__() takes exactly 7 arguments (2 given)
  #url = EasyUrl(url)
  
  # works fine, I don't know why. Except that I can't access
  # the tuples attributes in the class definition
  url = parse_url(url) 
  
  print url.scheme # Works fine
  
  url.set_scheme_if_non() # Raises an error
  </code></pre>
  <h3>Output</h3>
  <pre><code>File &quot;/home/crispycret/easyurl.py&quot;, line 50, in &lt;module&gt;
    url.set_scheme_if_non() # Raises an error
  File &quot;/home/crispycret/easyurl.py&quot;, line 29, in set_scheme_if_non
    self.scheme = scheme
  
  AttributeError: can't set attribute
  </code></pre>