Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install rwproperty

How to install rwproperty

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install rwproperty
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.0 Available View build log
Windows (64-bit)
1.0 Available View build log
Mac OS X (10.5+)
1.0 Available View build log
Linux (32-bit)
1.0 Available View build log
Linux (64-bit)
1.0 Available View build log
 
License
ZPL
Imports
Lastest release
version 1.0 on Jan 5th, 2011

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.

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.