title: Correct usage of a getter/setter for dictionary values
body: <p>I'm pretty new to Python, so if there's anything here that's flat-out bad, please point it out.</p>
  
  <p>I have an object with this dictionary:</p>
  
  <pre><code>traits = {'happy': 0, 'worker': 0, 'honest': 0}
  </code></pre>
  
  <p>The value for each trait should be an int in the range 1-10, and new traits should not be allowed to be added. I want getter/setters so I can make sure these constraints are being kept. Here's how I made the getter and setter now: </p>
  
  <pre><code>def getTrait(self, key):
      if key not in self.traits.keys():
          raise KeyError
  
      return traits[key]
  
  def setTrait(self, key, value):
      if key not in self.traits.keys():
          raise KeyError
  
      value = int(value)
  
      if value &lt; 1 or value &gt; 10:
          raise ValueError
  
      traits[key] = value
  </code></pre>
  
  <p>I read on <a href="http://tomayko.com/writings/getters-setters-fuxors" rel="noreferrer">this website</a> about the <code>property()</code> method. But I don't see an easy way to make use of it for getting/setting the values inside the dictionary. Is there a better way to do this? Ideally I would like the usage of this object to be <code>obj.traits['happy'] = 14</code>, which would invoke my setter method and throw a ValueError since 14 is over 10. </p>