IPython Documentation

Table Of Contents

Previous topic

utils.attic

Next topic

utils.codeutil

This Page

utils.autoattr

Module: utils.autoattr

Inheritance diagram for IPython.utils.autoattr:

Descriptor utilities.

Utilities to support special Python descriptors [1,2], 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.

A special ResetMixin class is provided to add a .reset() method to users who may want to have their objects capable of resetting these computed properties to their ‘untriggered’ state.

References

[1] How-To Guide for Descriptors, Raymond Hettinger. http://users.rcn.com/python/download/Descriptor.htm

[2] Python data model, http://docs.python.org/reference/datamodel.html

Notes

This module is taken from the NiPy project (http://nipy.sourceforge.net/nipy/stable/index.html), and is BSD licensed.

Authors

  • Fernando Perez.

Classes

OneTimeProperty

class IPython.utils.autoattr.OneTimeProperty(func)

Bases: object

A descriptor to make special properties that become normal attributes.

This is meant to be used mostly by the auto_attr decorator in this module.

__init__(func)

Create a OneTimeProperty instance.

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.

ResetMixin

class IPython.utils.autoattr.ResetMixin

Bases: object

A Mixin class to add a .reset() method to users of OneTimeProperty.

By default, auto attributes once computed, become static. If they happen to depend on other parts of an object and those parts change, their values may now be invalid.

This class offers a .reset() method that users can call explicitly when they know the state of their objects may have changed and they want to ensure that all their special attributes should be invalidated. Once reset() is called, all their auto attributes are reset to their OneTimeProperty descriptors, and their accessor functions will be triggered again.

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

reset()

Reset all OneTimeProperty attributes that may have fired already.

Function

IPython.utils.autoattr.auto_attr(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):
...     @auto_attr
...     def a(self):
...         return 99
...
>>> x = MagicProp()
>>> 'a' in x.__dict__
False
>>> x.a
99
>>> 'a' in x.__dict__
True