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

Grant Edwards grante at visi.com
Thu Jul 22 12:24:03 EDT 2004


On 2004-07-22, Richard Oudkerk <oudkerk at ma.man.ac.uk> 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!!
                               visi.com            



More information about the Python-list mailing list