tempvars API

class tempvars.TempVars(names=None, starts=None, ends=None, restore=True)

Context manager for handling temporary variables at the global scope.

WILL NOT WORK PROPERLY unless used as a context manager!!

CAN ONLY BE USED at global scopes (Python/IPython REPL, Jupyter notebook, etc.)

Parameters:
  • nameslist of str - Variables will be treated as temporary if their names test equal to any of these items.
  • startslist of str - Variables will be treated as temporary if their names start with any of these patterns (tested with .startswith(starts[i])).
  • endslist of str - Variables will be treated as temporary if their names end with any of these patterns (tested with .endswith(ends[i])).
  • restorebool - If True, any variables hidden from the namespace upon entry into the with suite are restored to the namespace upon exit. If False, no variables are restored.

The TempVars instance can be bound in the with statement for access to stored variables, etc.:

>>> with TempVars(names=['abcd']) as tv:
...     pass

See the usage examples page for more information.

Class Members

These objects are accessible via the instance bound as part of the with statement (tv from the above snippet). All are constructed using attr.ib().

names

list of str - All variable names passed to names.

starts

list of str - All passed .startswith matching patterns.

ends

list of str - All passed .endswith matching patterns.

restore

bool flag indicating whether to restore the prior namespace contents. Can be changed within the with suite.

stored_nsvars

dict container for preserving variables masked from the namespace, along with their associated values.

retained_tempvars

dict container for storing the temporary variables discarded from the namespace after exiting the with block.