METADATA 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. Metadata-Version: 2.0
  2. Name: classproperty
  3. Version: 1.0
  4. Summary: Class properties
  5. Home-page: http://www.z3lab.org/sections/blogs/philipp-weitershausen/2006_05_29_pycon-06-lightning-talk
  6. Author: Philipp von Weitershausen
  7. Author-email: philipp@weitershausen.de
  8. License: ZPL
  9. Download-URL: http://codespeak.net/svn/user/philikon/classproperty#egg=classproperty-dev
  10. Platform: UNKNOWN
  11. Class properties
  12. ================
  13. :Author: Philipp von Weitershausen
  14. :Email: philikon@philikon.de
  15. :License: Zope Public License, v2.1
  16. Motivation
  17. ----------
  18. Using method decorators and descriptors like ``property``, we can
  19. easily create computed attributes:
  20. >>> class JamesBrown(object):
  21. ... @property
  22. ... def feel(self):
  23. ... return self._feel
  24. An attribute like this cannot be written, though. You would have to
  25. do something like this:
  26. >>> class JamesBrown(object):
  27. ... def _getFeel(self):
  28. ... return self._feel
  29. ... def _setFeel(self, feel):
  30. ... self._feel = feel
  31. ... feel = property(_getFeel, _setFeel)
  32. The problem with this approach is that it leaves the getter and setter
  33. sitting around in the class namespace. It also lacks the compact
  34. spelling of a decorator solution. To cope with that, some people like
  35. to write:
  36. >>> class JamesBrown(object):
  37. ... @apply
  38. ... def feel():
  39. ... def get(self):
  40. ... return self._feel
  41. ... def set(self, feel):
  42. ... self._feel = feel
  43. ... return property(get, set)
  44. This spelling feels rather cumbersome, apart from the fact that
  45. ``apply`` is `going to go away`_ in Python 3000.
  46. .. _going to go away: http://www.python.org/peps/pep-3000.html#id24
  47. Goal
  48. ----
  49. There should be a way to declare a read & write property and still use
  50. the compact and easy decorator spelling. The read & write properties
  51. should be as easy to use as the read-only property. We explicitly
  52. don't want that immediately called function that really just helps us
  53. name the attribute and create a local scope for the getter and setter.
  54. Class properties
  55. ----------------
  56. Class properties let you define properties via the ``class``
  57. statement. You define a dynamic property as if you were implementing
  58. a class. This works like this:
  59. >>> from classproperty import classproperty
  60. >>> class JamesBrown(object):
  61. ... class feel(classproperty):
  62. ... def __get__(self):
  63. ... return self._feel
  64. ... def __set__(self, feel):
  65. ... self._feel = feel
  66. >>> i = JamesBrown()
  67. >>> i.feel
  68. Traceback (most recent call last):
  69. ...
  70. AttributeError: 'JamesBrown' object has no attribute '_feel'
  71. >>> i.feel = "good"
  72. >>> i.feel
  73. 'good'
  74. Of course, deleters are also possible:
  75. >>> class JamesBrown(object):
  76. ... class feel(classproperty):
  77. ... def __get__(self):
  78. ... return self._feel
  79. ... def __set__(self, feel):
  80. ... self._feel = feel
  81. ... def __delete__(self):
  82. ... del self._feel
  83. >>> i = JamesBrown()
  84. >>> i.feel = "good"
  85. >>> del i.feel
  86. >>> i.feel
  87. Traceback (most recent call last):
  88. ...
  89. AttributeError: 'JamesBrown' object has no attribute '_feel'