How to get the 'name' of an int

Hans Brand Hans.Brand at BrandInnovators.com
Fri Feb 28 05:05:30 EST 2003


"Erik Max Francis" <max at alcyone.com> wrote in message
news:3E5F3258.F7209964 at alcyone.com...
> Hans Brand wrote:
>
> > I would like to get the 'name' of a variable to do something like
> > this:
> >
> > 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?
>
> There is no guaranteed way to do this, as names are not unique, only
> objects are.  Consider:
>
> causea = causeb = 11
>
> If you ask for the name referring to 11, what should it be then?
>
> The long answer is that you might be able to get what you want by
> checking the values of the locals/globals dictionary and then seeing
> what keys map to them.  The short answer is that if you're trying to do
> this you're probably operating under the influence of a design flaw
> that's making you think this is a good idea.
>
> Why do you think you want this?
>
I just wondered if I could write something like:

for item in (a long list of variables):
    print "%s = %s" %(name(item), item)

without having to know each name of each variable






More information about the Python-list mailing list