83. attributes — Attributes — pyFormex 1.0.5 documentation

# 83. `attributes` — Attributes

This module defines a general class for adding extra attributes to other objects without cluttering the name space.

class `attributes.` `Attributes`(data={}, default=None)

  A general class for holding attributes.

  This class is a versatile dict class for objects that need a customizable set of attributes, while avoiding a wildly expanding name space.

  The class has dict type and object/attribute type syntax. Furthermore, an instance can be called as a function to populate or update its contents. Giving an attribute the value None removes it from the dict. Any non-existing attribute returns None.

  Parameters:

  - data: a dict or any other object that can be used to initialize a `Dict`. This provides the data stored in the Attributes.
  - default: either another Attributes instance, or None. If another Attributes is specified, keys not existing in self will be looked up in default and that value will be returned.

  Example:

      >>> A = Attributes()
      >>> print(A)
      {}
      >>> A(color='red',alpha=0.7,ontop=True)
      >>> print(A)
      {'alpha': 0.7, 'color': 'red', 'ontop': True}
      >>> A.ontop = None
      >>> A.alpha = 0.8
      >>> print(A)
      {'alpha': 0.8, 'color': 'red'}
      >>> B = Attributes({'color':'green'},default=A)
      >>> print(B)
      {'color': 'green'}
      >>> print(B.color,B.alpha)
      green 0.8
      >>> A.clear()
      >>> print(A)
      {}
      >>> print(B.color,B.alpha)
      green None
      >>> B(color=None,alpha=1.0)
      >>> print(B)
      {'alpha': 1.0}
      >>> B['alpha'] = None
      >>> print(B)
      {}