The goal is to create a mock class which behaves like a db resultset.
So for example, if a database query returns, using a dict expression, {\'ab\':100, \'cd\'
Here is a solution that:
After the class has been defined, you just do this to add a property to it dynamically:
setattr(SomeClass, 'propertyName', property(getter, setter))
Here is a complete example, tested in Python 3:
#!/usr/bin/env python3
class Foo():
pass
def get_x(self):
return 3
def set_x(self, value):
print("set x on %s to %d" % (self, value))
setattr(Foo, 'x', property(get_x, set_x))
foo1 = Foo()
foo1.x = 12
print(foo1.x)