Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def set_properties(names_to_docs):
- def decorator(cls):
- for name, doc in names_to_docs.items():
- prop = property((lambda self: getattr(self, '_{}'.format(name))),
- (lambda self, val: setattr(self, '_{}'.format(name), val),
- doc=doc)
- setattr(cls, name, prop)
- return cls
- return decorator
- >>> @set_properties({'a': 'This is a', 'b': 'This is b'})
- >>> class Test:
- ... def __init__(self):
- ... self._a = 1
- ... self._b = 2
- ...
- >>> print(Test.a.__doc__)
- This is a
- >>> Test().a
- 1
Add Comment
Please, Sign In to add comment