Little Q: how to print a variable's name, not its value?

Ron_Adam radam2 at tampabay.rr.com
Tue Mar 29 16:34:39 CEST 2005


On 28 Mar 2005 23:01:34 -0800, "Dan Bishop" <danb_83 at yahoo.com> wrote:

>>>> def print_vars(vars_dict=None):
>...    if vars_dict is None:
>...       vars_dict = globals()
>...    for var, value in vars_dict.items():
>...       print '%s = %r' % (var, value)
>...
>>>> myPlace = 'right here'
>>>> myTime = 'right now'
>>>> print_vars()
>print_vars = <function print_vars at 0x401e0d84>
>__builtins__ = <module '__builtin__' (built-in)>
>myTime = 'right now'
>myPlace = 'right here'
>__name__ = '__main__'
>__doc__ = None


Fred = 5
John = 8
Winner = John
 
Both John and Winner are pointing to the literal '8'.

Mixing data and program code, ie.. variable names as data, is not a
good idea.

Dictionaries are one of pythons best features. ;)


Ron




More information about the Python-list mailing list