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

Bill Mill bill.mill at
Thu Mar 31 10:33:10 CEST 2005

On 31 Mar 2005 08:13:30 GMT, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> stewart.midwinter at 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.
> The key here is to understand the difference between languages like C where
> a variable is a container used to store a value and Python where variables
> are simply the binding of names to objects.

I don't see any technical barrier to implementing a name() function,
except that either the current CPython doesn't allow this particular
reflection from python, or it's difficult to do. Just because a
variable is simply a name binding doesn't mean Python couldn't return
the name of the binding.

I'm not definitely saying that I *want* an easy name() function -
there's a lot of potential for abuse - but I would at least like to
see it accomplished before I decide whether I like it or not.

Bill Mill
bill.mill at

More information about the Python-list mailing list