Modules¶
Module Objects¶
Accretive modules have an interface nearly equivalent to
types.ModuleType, i.e., standard Python modules. However, once an
attribute has been assigned, it cannot be reassigned or deleted. This protects
accretive modules from accidental (or unsophisticated malicious) tampering.
>>> from accretive import Module
Initialization¶
While modules are typically initialized during import of their sources, they may also be created dynamically. As with standard Python modules, a name is required when dynamically creating a module.
>>> m = Module( 'foo' )
>>> m
<module 'foo'>
Immutability¶
Existing attributes cannot be reassigned.
>>> m.__name__
'foo'
>>> m.__name__ = 'bar'
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutability: Could not assign or delete existing attribute '__name__'.
Or deleted.
>>> del m.__name__
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutability: Could not assign or delete existing attribute '__name__'.
Attribute Assignment¶
However, new attributes can be assigned.
>>> m.__version__ = '1.0a3'
>>> '__version__' in vars( m )
True
Mass Reclassification¶
For cases where multiple modules should be reclassified, a convenience function is provided. This function looks for all modules in a dictionary, such as the attributes dictionary for another module, and reclassifies the modules to accretive modules.
from accretive import reclassify_modules
reclassify_modules( __name__ )