title: How to add property to a class dynamically?
body: <p>The goal is to create a mock class which behaves like a db resultset.</p>
  
  <p>So for example, if a database query returns, using a dict expression, <code>{'ab':100, 'cd':200}</code>, then I would like to see: </p>
  
  <pre><code>&gt;&gt;&gt; dummy.ab
  100
  </code></pre>
  
  <p>At first I thought maybe I could do it this way:</p>
  
  <pre><code>ks = ['ab', 'cd']
  vs = [12, 34]
  class C(dict):
      def __init__(self, ks, vs):
          for i, k in enumerate(ks):
              self[k] = vs[i]
              setattr(self, k, property(lambda x: vs[i], self.fn_readyonly))
  
      def fn_readonly(self, v)
          raise "It is ready only"
  
  if __name__ == "__main__":
      c = C(ks, vs)
      print c.ab
  </code></pre>
  
  <p>but <code>c.ab</code> returns a property object instead.</p>
  
  <p>Replacing the <code>setattr</code> line with <code>k = property(lambda x: vs[i])</code> is of no use at all.</p>
  
  <p>So what is the right way to create an instance property at runtime?</p>
  
  <p>P.S. I am aware of an alternative presented in <a href="https://stackoverflow.com/questions/371753/how-is-the-getattribute-method-used"><em>How is the <code>__getattribute__</code> method used?</em></a></p>