Python’s properties are very useful constructs that allows one to define getter and setter methods, while retaining a attribute interface. propertylib offers an alternate and way to define properties using the class syntax and also provide few specialized properties implementations that simplify some common use-cases.
The standard way to define a property with setter and getter methods is cumbersome and clutters the class namespace
>>> class Cls(object):
... def _answer_get(self):
... try:
... return self._answer
... except AttributeError:
... return 42
...
... def _answer_set(self, value):
... self._answer = value
...
... answer = property(_answer_get, _answer_set)
propertylib provides a more convenient method, exploiting Python’s metaclass mechanism
>>> from propertylib import *
>>> class Cls2(object):
... class answer(std_property):
... def fget(self):
... try:
... return self._answer
... except AttributeError:
... return 42
...
... def fset(self, value):
... self._answer = value
The new notation has less boilerplate code and does not clutter the class namespace with the two (usually) unecessary methods _answer_get and _answer_set.
Both ways are equivalent, for getters
>>> x = Cls(); x.answer
42
>>> y = Cls2(); y.answer
42
and for setters
>>> x.answer = 21; x.answer
21
>>> y.answer = 21; y.answer
21
The std_property class also provides a way to define the deleter, by defining the fdel method analogously as before.
We also provide specialized properties that simplify usage in some common use cases. See the documentation on default_property, auto_property, and cache_property.
Creates property objects with setters, getters and deleters using a class notation.
Examples
>>> class Cls(object):
... class answer(std_property):
... def fget(self):
... try:
... return self._answer
... except AttributeError:
... return 42
...
... def fset(self, value):
... self._answer = value
...
>>> x = Cls(); x.answer
42
>>> x.answer = 12; x.answer
12
A property that returns a default value, until it is manually set by the user.
| Parameters : | value : object
|
|---|
Examples
>>> class Cls(object):
... answer = default_property(42)
>>> x = Cls(); x.answer
42
>>> x.answer = 21; x.answer
21
A property that computes its value from ‘func()’ until the attribute is manually set by the user.
| Parameters : | func : callable
|
|---|
Examples
Defines a class with an auto_property
>>> class Cls(object):
... @auto_property
... def answer(self):
... return 42
Instantiate the object. The answer attribute already has a default value computed by the corresponding answer function.
>>> x = Cls(); x.answer
42
>>> x.answer = 12; x.answer
12
A property that implements an automatic cache of its value. It is useful for immutable objects that perform expensive computations or to provide default values for attributes that somehow depends on the state of the object. The default constructor allows for a getter method which computes the value in cache and a setter that allows the user to override the cache value.
| Parameters : | fget : callable
|
|---|
Notes
The user can change explicitly some value in the property’s cache by calling the Class.property.save() method. It can also clear the cache using Class.property.clear().
Examples
Consider the class with a cache_property
>>> class Cls(object):
... @cache_property
... def answer(self):
... print 'Deep thought is computing...'
... return 42
The answer method is only called once, and its value is stored.
>>> x = Cls(); x.answer
Deep thought is computing...
42
>>> x.answer
42
This value can be overridden by assignment
>>> x.answer = 21; x.answer
21
Cache can be erased by the reset method
>>> Cls.answer.clear(x); x.answer
Deep thought is computing...
42