title: Should I extract values from Python dictionaries into object attributes?
body: <p>I have a Python class that is initialized with a dictionary of settings, like this:</p>
  
  <pre><code>def __init__(self, settings):
      self._settings = settings
  </code></pre>
  
  <p>Settings dictionary contains 50-100 different parameters that are used quite a lot in other methods:</p>
  
  <pre><code>def MakeTea(self):
      tea = Tea()
      if self._settings['use_sugar']:
          tea.sugar_spoons = self._settings['spoons_of_sugar']
      return tea
  </code></pre>
  
  <p>What I want to know is whether it makes sense to preload all the params into instance attributes like this:</p>
  
  <pre><code>def __init__(self, settings):
      self._use_sugar = settings['use_sugar']
      self._spoons_of_sugar = settings['spoons_of_sugar']
  </code></pre>
  
  <p>and use these attributes instead of looking up dictionary values every time I need them:</p>
  
  <pre><code>def MakeTea(self):
      tea = Tea()
      if self._use_sugar:
          tea.sugar_spoons = _self._spoons_of_sugar
      return tea
  </code></pre>
  
  <p>Now, I am fairly new to Python and I worked mostly with compiled languages where it really is a no-brainer: access to instance fields will be much faster than looking up values from any kind of hashtable-based structure. However, with Python being interpreted and all, I'm not sure that I'll have any significant performance gain because at the moment I have almost no knowledge of how Python interpreter works. For all I know, using attribute name in code may involve using some internal dictionaries of identifiers in interpreted environment, so I gain nothing.</p>
  
  <p>So, the question: are there any significant performance benefits in extracting values from dictionary and putting them in instance attributes? Are there any other benefits or downsides of doing it? What's the good practice?</p>
  
  <p>I strongly believe that this is an engineering decision rather than premature optimization. Also, I'm just curious and trying to write decent Python code, so the question seems valid to me whether I actually need those milliseconds or not.</p>