UserSpace.new_cells#
- UserSpace.new_cells(name=None, formula=None, is_cached=True)[source]#
Create a new
Cellsobject in this space.Creates a cells object that acts as a callable formula with automatic result caching. The formula can be defined by passing a Python function, lambda expression, or formula string.
- Parameters:
name (str, optional) – Name for the cells. If omitted, the name of the function is used. If the function name is not valid for a cells name, an automatic name is assigned in the format
CellsNwhereNis an available number. Must be a valid Python identifier, and must not start with an underscore.formula (callable or str, optional) –
The formula definition. Can be:
A Python function (def or lambda)
A string containing a lambda expression
None to create an empty cells
is_cached (bool, optional) – Whether to cache calculation results. If
True(default), results are cached by arguments. IfFalse, the formula is recalculated on every call.
- Returns:
The newly created cells object
- Return type:
Example
Creating a cells object using new_cells:
>>> space = model.new_space('MySpace') >>> def present_value(t): ... return cashflow(t) / (1 + rate) ** t >>> space.new_cells(formula=present_value) <Cells Model1.MySpace.present_value(t)>
Above is equivalent to creating cells using a decorator:
>>> @mx.defcells ... def present_value(t): ... return cashflow(t) / (1 + rate) ** t <Cells Model1.MySpace.present_value(t)>
Creating a cells from a lambda function:
>>> space.new_cells('double', lambda x: x * 2) <Cells Model1.MySpace.double(x)>
Creating a cells without caching from a lambda function:
>>> space.new_cells('triple', lambda: x: x * 3, is_cached=False) <Cells Model1.MySpace.triple(x)>
See also
new_cells_from_module(): Create multiple cells from a moduleis_cached: Control caching behaviorset_formula(): Modify formula after creation