Dynamically adding a property to a class <p>This has been previously asked on Stack Overflow, but none of the answers seem to address exactly what I need to do. In my case, I want these dynamically-added properties to be a shortcut to store and read values from a database, so unfortunately it's not as easy <a href="https://stackoverflow.com/a/1355444/184363">as in this answer</a> (where a <code>lambda</code> function was used) or <a href="https://stackoverflow.com/q/10967551/184363">this one</a> (where values where stored in a dictionary): I must call other methods of the class.</p> <p>This is my attempt:</p> <pre><code>import operator class Foo(object): def get_value(self, name): # read and return value from database return -1 def set_value(self, name, value): # store value in database pass def add_metadata_property(name): getter = operator.methodcaller('get_value', name) setter = operator.methodcaller('set_value', name) # gets value at runtime setattr(Foo, name, property(getter, setter)) add_metadata_property('spam') f = Foo() f.spam # works! f.spam = 2 </code></pre> <p>The last line, however, raises:</p> <pre><code>Traceback (most recent call last): File "&lt;stdin&gt;", line 27, in &lt;module&gt; TypeError: methodcaller expected 1 arguments, got 2 </code></pre> <p>Any ideas on how to achieve this?</p>