Skip to main content

Class properties

Project description

Author:

Philipp von Weitershausen

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.

Class properties

Class properties let you define properties via the class statement. You define a dynamic property as if you were implementing a class. This works like this:

>>> from classproperty import classproperty
>>> class JamesBrown(object):
...     class feel(classproperty):
...         def __get__(self):
...             return self._feel
...         def __set__(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'

Of course, deleters are also possible:

>>> class JamesBrown(object):
...     class feel(classproperty):
...         def __get__(self):
...             return self._feel
...         def __set__(self, feel):
...             self._feel = feel
...         def __delete__(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'

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

classproperty-1.0.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

classproperty-1.0-py2.4.egg (2.8 kB view details)

Uploaded Source

File details

Details for the file classproperty-1.0.tar.gz.

File metadata

  • Download URL: classproperty-1.0.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for classproperty-1.0.tar.gz
Algorithm Hash digest
SHA256 c2caea61ed431ade216aed9f5253225a23b383252cc954fb254ea9487cea7056
MD5 5abd0527fcf8884b7d07203a0967a433
BLAKE2b-256 2601d0590085d6dcf0182a6ad43b89826726f5d01b6679745c468c78c6b74197

See more details on using hashes here.

File details

Details for the file classproperty-1.0-py2.4.egg.

File metadata

File hashes

Hashes for classproperty-1.0-py2.4.egg
Algorithm Hash digest
SHA256 65d6faa6c6af6882c3f794ccef46f303ddcb22782a6a303c0ddda0cf2ca55e35
MD5 8335f5adfd32825066b34c675fc36ff6
BLAKE2b-256 39165766696472cab336f44ce826384e0a183e0018e0c37079b11a23df93cf04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page