[Tutor] printing variable name and value?

Emile van Sebille emile@fenx.com
Sun, 28 May 2000 12:12:17 -0700


Does this do it?

def show_vars(varlist, namespace):
  for var in varlist:
    print "%s = %s" % (var, namespace[var])


>>> a = 1
>>> b = 2
>>> show_vars(('a','b'),locals())
a = 1
b = 2
>>>

Emile van Sebille
emile@fenx.com
-------------------


----- Original Message -----
From: Borgulya Gabor <borgulya@pons.sote.hu>
To: <tutor@python.org>
Sent: Sunday, May 28, 2000 11:35 AM
Subject: [Tutor] printing variable name and value?


> Hi!
>
> When I am testing my scripts, I often need the following line:
>
> print 'x =',x
>
> But as I like verbose variable names I hate to type them twice.
> I wanted to write a small function that works like this:
>
> >>> x=5
> >>> print_variable('x')
> x = 5
> >>>
>
> But I was unable to do it. If I pass the print_variable function the
value
> of x, like print_variable(x), I can not access the variable name. And
if I
> pass it the variable name, like above, I can not access the value of
the
> variable, because I am in a new name space.
>
> Do I need such complicated methods as Traceback?
>
> Gabor
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>