dipy logo

Site Navigation

NIPY Community

Table Of Contents

Previous topic

dipy.core.meshes

Next topic

dipy.core.profile

dipy.core.onetime

Descriptor support for NIPY.

Utilities to support special Python descriptors ([R3], [R4]), in particular the use of a useful pattern for properties we call ‘one time properties’. These are object attributes which are declared as properties, but become regular attributes once they’ve been read the first time. They can thus be evaluated later in the object’s life cycle, but once evaluated they become normal, static attributes with no function call overhead on access or any other constraints.

References

[R3]How-To Guide for Descriptors, Raymond

Hettinger. http://users.rcn.com/python/download/Descriptor.htm

[R4]Python data model, http://docs.python.org/reference/datamodel.html
dipy.core.onetime.OneTimeProperty

A descriptor to make special properties that become normal attributes.

dipy.core.onetime.setattr_on_read(func)

Decorator to create OneTimeProperty attributes.

Parameters :

func : method

The method that will be called the first time to compute a value. Afterwards, the method’s name will be a standard attribute holding the value of this computation.

Examples

>>> class MagicProp(object):
...     @setattr_on_read
...     def a(self):
...         return 99
...     
>>> x = MagicProp()
>>> 'a' in x.__dict__
False
>>> x.a
99
>>> 'a' in x.__dict__
True