Little Q: how to print a variable's name, not its value?

Bill Mill bill.mill at gmail.com
Thu Mar 31 03:37:19 EST 2005


On Thu, 31 Mar 2005 03:33:10 -0500, Bill Mill <bill.mill at gmail.com> wrote:
> On 31 Mar 2005 08:13:30 GMT, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> > stewart.midwinter at gmail.com wrote:
> >
> > > But surely if you create an integer object and assign it a value, e.g.
> > > a = 3,
> > > why shouldn't Python be able to tell you something like the following:
> > > name(a)  >>> 'a'
> > > ?
> >
> > But why should it return 'a' and not one of these?
> 
> Because the python interpreter certainly knows the name of all the
> variables used in a python program. So, a name() function as discussed
> here should return the name of the exact variable passed in as input,
> not any variable which refers to the value 3 (which is of course
> constant). We're imagining a new function, not discussing yours.

Reason #312 not to post at 3:30 AM:

Ok, the variable name is evaluated before being passed to the
function. Thus, it would require some pretty heavy interpreter
trickery to implement. I take back what I said.

Humbly,
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list