Sometimes you want the value of a variable to be related to its name in the source code. For example, the variable x should have the value "var x". This is especially useful when you perform symbolic computations with python (with the sympy module for example). In standard python, the only way to obtain this behavior is to repeat the name of the variable in the call to the function which creates the value. For example
x, y, z = create_values(varnames = ('x', 'y', 'z')).
This snippet defines a decorator
with_varnames which can be applied to the value creation function and which extracts the variable names from the python code at the point where the function is called. One only needs to write
x, y, z = create_values() to achieve the same effect.
This snippet was tested under python 2.6 and 3.1.
Edited 2015 Jan 6th with docstrings and version number. Python 2.7 and 3.4.
Edited by Gribouillis