Suppose I have a class like this:
class Alphabet(object):
__init__(self):
self.__dict = {'a': 1, 'b': 2, ... 'z': 26}
@property
def a(self):
return self.__dict['a']
@property
def b(self):
return self.__dict['b']
...
@property
def z(self)
return self.__dict['z']
This would be a long and cumbersome task to define and it seems highly redundant. Is there a way to dynamically create these properties? I know you can dynamically create attributes with the built-in setattr, but I want to have control over read/write/delete access (for this reason I want to use a property). Thanks!
@property\ndef something()is equivalent tosomething = property(something). Which is equivalent to doing (in this case)self.setattr('a', property(lambda self: self.__dict['a']))