Little Q: how to print a variable's name, not its value?
Bill Mill
bill.mill at gmail.com
Tue Mar 29 14:58:45 EST 2005
<snip>
> >>
> >> Fred = 5
> >> John = 8
> >> Winner = John
> >>
> >> Both John and Winner are pointing to the literal '8'.
> >
> >ummm, yes, of course they are. What's your point?
>
> Hi Bill,
>
> My point is if you look up the name and print it, you may get.
>
> Instead of:
>
> Fred has 5 points
> John has 8 points
>
> You could get:
>
> Fred has 5 points
> Winner has 8 points
>
> 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.
>
> >> Mixing data and program code, ie.. variable names as data, is not a
> >> good idea.
> >
> >Down with eval! Exile exec! A pox on both their houses!
> >
> >(i.e. I respectfully disagree that mixing data with program code is a bad idea)
>
> (I respectfully acknowledged your opinion.)
>
> To be fair, it's not always bad. But in most cases it is better not
> too.
>
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.
<code arguing against maintining an ad-hoc locals()>
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 was too glib in my response to you. I intended no disrespect, only silliness.
Peace
Bill Mill
bill.mill at gmail.com
More information about the Python-list
mailing list