Using Python I want to create a property in a class, but having the name of it in a string. Normally you do:
blah = property(get_blah, set_blah, del_blah, "bleh blih")
where get_, set_ and del_blah have been defined accordingly. I've tried to do the same with the name of the property in a variable, like this:
setattr(self, "blah", property(self.get_blah, self.set_blah, self.del_blah, "bleh blih"))
But that doesn't work. The first case blah returns the value of the property, on the second case, it returns a property, that is <property object at 0xbb1aa0>
. How should I define it so it works?
As much I would say, the difference is, that in the first version, you change the classes attribute blah to the result of property and in the second you set it at the instance (which is different!).
How about this version:
setattr(MyClass, "blah", property(self.get_blah, self.set_blah,
self.del_blah, "bleh blih"))
you can also do this:
setattr(type(self), "blah", property(self.get_blah, self.set_blah,
self.del_blah, "bleh blih"))