- Email:
- License:
Zope Public License, v2.1
Motivation
Using method decorators and descriptors like property, we can easily create computed attributes:
>>> class JamesBrown(object): ... @property ... def feel(self): ... return self._feel
An attribute like this cannot be written, though. You would have to do something like this:
>>> class JamesBrown(object): ... def _getFeel(self): ... return self._feel ... def _setFeel(self, feel): ... self._feel = feel ... feel = property(_getFeel, _setFeel)
The problem with this approach is that it leaves the getter and setter sitting around in the class namespace. It also lacks the compact spelling of a decorator solution. To cope with that, some people like to write:
>>> class JamesBrown(object): ... @apply ... def feel(): ... def get(self): ... return self._feel ... def set(self, feel): ... self._feel = feel ... return property(get, set)
This spelling feels rather cumbersome, apart from the fact that apply is going to go away in Python 3000.
Goal
There should be a way to declare a read & write property and still use the compact and easy decorator spelling. The read & write properties should be as easy to use as the read-only property. We explicitly don’t want that immediately called function that really just helps us name the attribute and create a local scope for the getter and setter.
Read & write property
Read & write properties work like regular properties. You simply define a method and then apply a decorator, except that you now don’t use @property but @getproperty to mark the getter and @setproperty to mark the setter:
>>> from rwproperty import getproperty, setproperty >>> class JamesBrown(object): ... @getproperty ... def feel(self): ... return self._feel ... @setproperty ... def feel(self, feel): ... self._feel = feel>>> i = JamesBrown() >>> i.feel Traceback (most recent call last): ... AttributeError: 'JamesBrown' object has no attribute '_feel'>>> i.feel = "good" >>> i.feel 'good'
The order in which getters and setters are declared doesn’t matter:
>>> from rwproperty import getproperty, setproperty >>> class JamesBrown(object): ... @setproperty ... def feel(self, feel): ... self._feel = feel ... @getproperty ... def feel(self): ... return self._feel>>> i = JamesBrown() >>> i.feel = "good" >>> i.feel 'good'
Of course, deleters are also possible:
>>> from rwproperty import delproperty >>> class JamesBrown(object): ... @setproperty ... def feel(self, feel): ... self._feel = feel ... @getproperty ... def feel(self): ... return self._feel ... @delproperty ... def feel(self): ... del self._feel>>> i = JamesBrown() >>> i.feel = "good" >>> del i.feel >>> i.feel Traceback (most recent call last): ... AttributeError: 'JamesBrown' object has no attribute '_feel'
Edge cases
There might be a case where you’re using a flavour of read & write properties and already have a non-property attribute of the same name defined:
>>> class JamesBrown(object): ... feel = "good" ... @getproperty ... def feel(self): ... return "so good" ... Traceback (most recent call last): ... TypeError: read & write properties cannot be mixed with other attributes except regular property objects.
Metadata
Release files for rwproperty 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| rwproperty-1.0.tar.gz | 2.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rwproperty-1.0-py2.4.egg | Legacy Egg format | - | - | Details |
Total release size: 6.9 kB
Release files / rwproperty-1.0.tar.gz
| Download URL | rwproperty-1.0.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49742642f2b5e6b0861b35a43ceda2b1aad3c5173dbd6f3e4ce1cb10037f8416
|
|
BLAKE2b-256 checksum How to use checksums |
71fce3d922fc07db990f9aa235a2b1c690a167a9403aadc6ac30afb59e7cc0b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / rwproperty-1.0-py2.4.egg
| Download URL | rwproperty-1.0-py2.4.egg |
|---|---|
| Size | 3.9 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
e50f3fe67bf418de845a5cf95c1695d703d3d84984c31e46a1b301598846cfac
|
|
BLAKE2b-256 checksum How to use checksums |
b56b76cd9560ad57172c1f331d9e9ae58c3f76078fbe35c811661870d5d94de1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |