getting the name of a variable

brueckd at brueckd at
Thu Dec 6 21:00:51 CET 2001

On 6 Dec 2001, Sandy Norton wrote:

> When I'm debugging I'm always sticking stuff like "print 'x:', x" in
> my code. So is there a handy function that will print a variable name
> and value such that:
> >>> def print_var_name_and_value(var):
>             "prints variable name : value"
>             <implemention>
> >>> variable = 'me var'
> >>> print_var_name_and_value(variable)
> variable : me var

Hi Sandy,

Others will probably tell you why a Python value doesn't know which
variables reference it, but I think all you really want is a
quick-and-dirty way to generate that print statement, right? What editor
are you using? Many editors have support for macros, and for binding a
macro to a keypress, so it should be pretty easy to have your editor do
this for you, ie - you type the variable name, hit a key, and the editor
replaces it with the print statement that includes the variable name and
the value it references.

If this is really all you need, let us know what editor you're using and
I'm sure someone here will be able to help you write a macro to do that
for you.


More information about the Python-list mailing list