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

R

Ron_Adam

Here is a rough attempt at printing the names of a variable. It will pick
up several names where appropriate, but deliberately doesn't attempt to
get all possible names (as you say, that could result in endless loops).
In particular, for the Fred=5/John=8/Winner=8 example it will only find
one of John or Winner since it only picks at most one match from each dict
or list. It doesn't yet manage to correctly lookup attributes (e.g. slots)
when they aren't stored in a __dict__, nor does the output distinguish
between dictionary keys and values (so encodings.cp437.encoding_map[8]
below actually refers to the key not the value).


Here's what I've been working on. It still has some glitches in it
but I think it has potential as a instructional/diagnostict tool

I'm going to repost this with the source as it's own topic, maybe it
can be developed further. :)

Cheers,
Ron



IDLE 1.1.1c1 ==== No Subprocess ====[globals]
__builtins__ --> <module>
__doc__ --> <docstring>
__file__ --> C:\Python24\Lib\idlelib\idle.pyw
__name__ --> __main__
idlelib --> <module>
pnames --> said:
John = 8
Fred = 6
Winner = John
players = [John, Fred]
pnames()
[globals]
__builtins__ --> <module>
__doc__ --> <docstring>
__file__ --> C:\Python24\Lib\idlelib\idle.pyw
__name__ --> __main__
Fred --> 6
idlelib --> <module>
John --> 8
players --> [8, 6]
pnames --> <function>
Winner --> 8
Paused

Both winner and John point to the litteral '8', but since the number
'8' can never be changed, it doesn't matter, but you can look up the
number '8' and find both John and Winner, but changing one, doesn't
change the other.

[globals]
__builtins__ --> <module>
__doc__ --> <docstring>
__file__ --> C:\Python24\Lib\idlelib\idle.pyw
__name__ --> __main__
Fred --> 6
idlelib --> <module>
John --> 9
players --> [8, 6]
pnames --> <function>
Winner --> 8
Paused

Winner didn't change it's value.

[globals]
__builtins__ --> <module>
__doc__ --> <docstring>
__file__ --> C:\Python24\Lib\idlelib\idle.pyw
__name__ --> __main__
Fred --> 6
idlelib --> <module>
John --> 9
players --> [8, 6] <-- scores
pnames --> <function>
scores --> [8, 6] <-- players
Winner --> 8
Paused
Here, players and scores are both mutable, changeing them will change
the other and so it shows that the list [8,6] has more than one name.


Cheers,
Ron
 
S

stewart.midwinter

Bengt wrote:
"The way you use those words makes me wonder: "assign _it_"?? Which
'it'? "

- it's probably evident to any close observer that my understanding of
objects is superficial, no doubt a reflection of the fact that I
started programming on punch cards a very long time ago. I use objects
every day, but I clearly need to do some further study in order to
develop my knowledge.

Thanks for your explanations.!
 
B

Bengt Richter

Bengt wrote:
"The way you use those words makes me wonder: "assign _it_"?? Which
'it'? "

- it's probably evident to any close observer that my understanding of
objects is superficial, no doubt a reflection of the fact that I
started programming on punch cards a very long time ago. I use objects
every day, but I clearly need to do some further study in order to
develop my knowledge.

Thanks for your explanations.!
You're welcome. But I started programming with flexowriter punched
paper tape before I got to punch cards, so either the prognosis is hopeful or
maybe you should take what I write with a grain or two of salt. Probably both ;-)

Regards,
Bengt Richter
 
J

John J. Lee

Restating: I'm doing some debugging of some code. I want to print out
the value of two variables whose names are known. Let's call them
myTime and myPlace.
[...]

Why not simply get your editor to insert the variable name twice? I
have that bound to C-c d in my .emacs.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,232
Messages
2,571,168
Members
47,803
Latest member
ShaunaSode

Latest Threads

Top