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

Ron_Adam radam2 at
Tue Mar 29 22:42:36 CEST 2005

On Tue, 29 Mar 2005 14:58:45 -0500, Bill Mill <bill.mill at>


>> Or something else depending on how many references you made to the
>> value 8.
>Yes, this is true, assuming that he looks for keys with the value 8 in
>locals(). It's not necessarily true if there's a way to ask python
>what the name of John is, which is what the OP was asking. I just
>wanted you to explicitly say what you were implying so that we could
>discuss it.

ok, :)

I should have explained my position better the first time. I was a bit
too brief.  

Unfortunately, while class's and functions have a __name__ attribute,
simple data types do not.

I've been playing around with a way to explore name spaces, but once
you drop into class's, and functions, the references can lead you into
an endless loops.  

>Yup, I meant to say that I disagree that mixing data with program code
>is *always* a bad idea. I had a "d'oh!" moment when I hit send.

I do that more often than I like.  Maybe I should have said 'is often'
not a good idea. 

>Naturally, I wasn't suggesting that anyone (shudder) do things like
>your examples of poor code. I had a much smaller point, about which I
>was not clear: Sometimes, it is handy to mix code and data. There
>*are* legitimate uses of reflection, eval, and exec.

I did't think you would suggest that, but I thought it was a good
chance to clarify what I meant the first time. If you missed my point,
then so did others. An obvious example is sometimes the best way to
demonstrate a basic concept.

There are good uses for eval and exec. Ultimately it's up to the
programmer to decide the best use of the tools.  I just like to keep
my tools(code), and my parts(data) in separate bins. ;)


More information about the Python-list mailing list