title: How to ensure keys (or values) are of a specified data type in a dict?
body: <p>My python program uses dicts and there are a massive amount of "if" statements just for checking the type of the retrieved value.</p>
  
  <p>I want to avoid this but instead do it a more programatically correct way.</p>
  
  <p>Here is an example:</p>
  
  <pre><code># golddb should only contain str keys and int values
  golddb = dict()
  
  def gainGold(playername):
    global golddb
    golddb[playername] += 1  # error may happen if I try to += a non-int type
    golddb[playername] = "hello"  # I want python to give an error when I try to assign a str to be a value in the dict
  </code></pre>