how can I get the name of a variable (or other object)?

Grant Edwards grante at
Thu Jul 22 18:24:03 CEST 2004

On 2004-07-22, Richard Oudkerk <oudkerk at> wrote:
> Try
> def printvars(varlist):
>      allvars = globals()
>      allvars.update(locals())
>      for var in varlist:
>          print [ n for n in allvars if allvars[n] is var ], "=>", var

I don't think the allvars.update(locals()) is doing anything
useful.  The locals it's adding are those belonging to
'printvars' and what one would probably want is the locals
belonging to the caller of printvar.

I like your solution of passing objects better than mine of
passing the name.

Grant Edwards                   grante             Yow!  HUGH BEAUMONT died
                                  at               in 1982!!

More information about the Python-list mailing list