Guest User

Untitled

a guest
Jan 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. def set_properties(names_to_docs):
  2. def decorator(cls):
  3. for name, doc in names_to_docs.items():
  4. prop = property((lambda self: getattr(self, '_{}'.format(name))),
  5. (lambda self, val: setattr(self, '_{}'.format(name), val),
  6. doc=doc)
  7. setattr(cls, name, prop)
  8. return cls
  9. return decorator
  10.  
  11. >>> @set_properties({'a': 'This is a', 'b': 'This is b'})
  12. >>> class Test:
  13. ... def __init__(self):
  14. ... self._a = 1
  15. ... self._b = 2
  16. ...
  17. >>> print(Test.a.__doc__)
  18. This is a
  19. >>> Test().a
  20. 1
Add Comment
Please, Sign In to add comment