10. attributes — Attributes¶
This module defines a general class for adding extra attributes to other objects without cluttering the name space.
- class attributes.Attributes(default_factory=<function Dict.returnNone>, *args, **kargs)[source]¶
- A general class for holding attributes. - This class is a versatile mapping class for objects that need a customizable set of attributes, while avoiding a wildly expanding name space. - The class derives from - Dictand therefore has key lookup via normal dict key mechanism or via attribute syntax or via function call. It also provides a default_factory to lookup missing keys.- The difference with the - Dictclass are:- The function call can also be used to populate or update the contents of the Mapping. 
- By default, a default_factory is set returning None for any missing key. 
- Giving an attribute the value None removes it from the Mapping. 
 - Parameters:
- default_factory (callable, optional) – If provided, missing keys will be looked up by a call to the default_factory. 
- args (other positional and keyword arguments) – Any other arguments are passed to the dict initialization. 
- kargs (other positional and keyword arguments) – Any other arguments are passed to the dict initialization. 
 
 - Notes - While setting a single item to None will remove the item from the Attributes, None values can be entered using the update() method. - The parameter order is different from previous implementation of this class. This was done for consistency with the Dict and CDict classes. - Examples - >>> A = Attributes() >>> A Attributes({}) >>> A(color='red',alpha=0.7,ontop=True) >>> A Attributes({'color': 'red', 'alpha': 0.7, 'ontop': True}) >>> A['alpha'] = 0.8 >>> A.color = 'blue' >>> A.ontop = None # remove 'ontop' >>> A Attributes({'color': 'blue', 'alpha': 0.8}) >>> A = Attributes({'alpha': 0.8, 'color': 'blue'}) >>> A.ontop is None True - Create another Attributes with A as default, override color: - >>> B = Attributes(default_factory=A, color='green') >>> B Attributes({'color': 'green'}) >>> B.color, B.alpha # alpha found in A ('green', 0.8) >>> A.clear() >>> A Attributes({}) >>> A['alpha'], A.alpha, A('alpha') # all mechanisms still working (None, None, None) >>> B['alpha'], B.alpha, B('alpha') (None, None, None) >>> B(color=None,alpha=1.0) # remove and change in 1 operation >>> B Attributes({'alpha': 1.0}) >>> B.update(color=None) # update can be used to enter None values. >>> B Attributes({'alpha': 1.0, 'color': None}) >>> B['alpha'] = None >>> B Attributes({'color': None}) 
 
  