Skip to content

varname

package

varname

Dark magics about variable names in python

Some helper functions builtin based upon core features

Classes
  • Wrapper A wrapper with ability to retrieve the variable name</>
Functions
  • debug(var, *more_vars, prefix, merge, repr, sep, vars_only) Print variable names and values.</>
  • exec_code(code, globals, locals, sourcefile, frame, ignore, **kwargs) Execute code where source code is visible at runtime.</>
  • jsobj(*args, **kwargs) (dict(str: any)) A wrapper to create a JavaScript-like object</>
  • register(cls_or_func, frame, ignore, multi_vars, raise_exc, strict) ( or ) A decorator to register varname to a class or function</>
module

varname.core

Provide core features for varname

Functions
  • argname(arg, *more_args, func, dispatch, frame, ignore, vars_only) (Union(ast, str, (ast or str, ...), , (union(ast, str, (ast or str, ...), ), ...))) Get the names/sources of arguments passed to a function.</>
  • nameof(var, *more_vars, frame, vars_only) (Union(str, (str, ...))) Get the names of the variables passed in</>
  • varname(frame, ignore, multi_vars, raise_exc, strict) (Union(str, (str or tuple, ...))) Get the name of the variable(s) that assigned by function call orclass instantiation. </>
  • will(frame, raise_exc) (str) Detect the attribute name right immediately after a function call.</>
module

varname.utils

Some internal utilities for varname

Classes
Functions
module

varname.ignore

The frame ignoring system for varname

There 4 mechanisms to ignore intermediate frames to determine the desired one so that a variable name should be retrieved at that frame.

  1. Ignore frames by a given module. Any calls inside it and inside its submodules will be ignored. A filename (path) to a module is also acceptable and recommended when code is executed by exec without module available.
  2. Ignore frames by a given pair of module and a qualified name (qualname). See 1) for acceptable modules. The qualname should be unique in that module.
  3. Ignore frames by a (non-decorated) function.
  4. Ignore frames by a decorated function. In this case, you can specified a tuple with the function and the number of decorators of it. The decorators on the wrapper function inside the decorators should also be counted.

Any frames in varname, standard libraries, and frames of any expressions like are ignored by default.

Classes
Functions