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

Grant Edwards grante at visi.com
Thu Jul 22 13:02:43 EDT 2004


On 2004-07-22, Grant Edwards <grante at visi.com> wrote:
> 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.

That would be:

        allvars.update(sys._getframe(1).f_locals) 

Better yet, print whether the object was found in the globals or
locals .
        
-- 
Grant Edwards                   grante             Yow!  I'LL get it!! It's
                                  at               probably a FEW of my
                               visi.com            ITALIAN GIRL-FRIENDS!!



More information about the Python-list mailing list