How to get the 'name' of an int

Rene Pijlman reageer.in at de.nieuwsgroep
Fri Feb 28 05:02:44 EST 2003


Hans Brand:
>I would like to get the 'name' of a variable to do something like this:

That's not possible, 0 or more names map to 1 object, there is
no reverse mapping (AFAIK). But...

>causea = 12
>causeb = 13
>for cause in (causea, causeb):
>    print "%s = %s" %(name(cause), cause)
>
>The result should be:
>causea = 12
>causeb = 13
>
>How can I achieve this?

it can be done like this:

causea = 1
causeb = 2
for cause in ("causea", "causeb"):
    print "%s = %d" % (cause, eval(cause))

And you could add some code to read the set of variables from a
namespace, instead of hardcoding it.

-- 
René Pijlman

Wat wil jij leren?  http://www.leren.nl




More information about the Python-list mailing list