Retrieving a variable's name.

Paul Rubin http
Mon Aug 20 23:48:10 EDT 2007


rodrigo <rodrigo.cr at gmail.com> writes:
> How would I go about retrieving a variable's name (not its value)? I
> want to write a function that, given a list of variables, returns  a
> string with each variable's name and its value, like:
> 
> a: 100
> b: 200
> 
> I get the feeling this is trivial, but I have been unable to find an
> answer on my own.

Why do you want to do that?  The reason you haven't found an answer is
that it's almost certainly not the right solution to whatever problem
you're trying to solve.

   '\n'.join('%s: %s'%(k,v) for k,v in locals().iteritems())

(untested) does something like what you're asking but I don't
advise it.  You should probably use a dictionary object instead.



More information about the Python-list mailing list