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

Bengt Richter bokr at oz.net
Thu Mar 31 03:33:25 EST 2005


On 30 Mar 2005 21:56:06 -0800, "stewart.midwinter at gmail.com" <stewart.midwinter at gmail.com> wrote:

>my god, I've created a monster!
>
>Maybe I should restate my original problem. Actually, the word
>'problem' is too strong. I had a little curiosity about whether I could
>write a couple of lines of code more succinctly, or more pythonically.
>  I didn't realize that this would trigger a discussion about mixing
>data and objects - though I can see the danger if you get into some
>situations. Hopefully mine is not one of those.
>
>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.
>
>#debug:
>if self.debug:
>   print "myTime = %s, myPlace = %s" % (myTime, myPlace)
>
>Notice that I had to type the variable's name once as an object, and
>once as the string representation of that object, i.e. the object's
>name.
>I wondered whether it might not be possible to do something like this:
>if self.debug:
>   print "%s = %s" % ( name(myTime), myTime )
>where 'name' is the method or trick I'm after.
Why isn't name(myTime) just 'myTime' ?
Which of the possible aliases would you like, if that's what you're looking for?

>

Anyway, if you just want a way of generating the string 'x = <the value of x>' without
typing 'x' twice, you can use a custom mapping class and a __getitem__ method to
do the work. E.g.,

 >>> class ShowEq(dict):
 ...     def __getitem__(self, name):
 ...         return '%s = %r'%(name, self.get(name, '<UNDEF!!>'))
 ...

Some variables
 >>> a,b = 'eigh', 'bee'

print them, using an instance of the mapping initialized with vars() which is the local namespace

 >>> print '%(a)s, %(b)s, %(ShowEq)s' % ShowEq(vars())
 a = 'eigh', b = 'bee', ShowEq = <class '__main__.ShowEq'>


Which does something like

    print "%(myTime)s = %s" % ( name(myTime), myTime )

I've forgotten who first did this, but I think it was before you could subclass dict ;-)
  
>Creating a dictionary is already more work than it's worth, not to
>mention some of the more involved solutions.   I'm left to conclude
>that it's not possible to do what I wanted with Python.  If that's the
>case, so be it and I'll move on to explore other curiosities.
Sometimes it's hard to get across exactly what it is you want, so don't give up until
you're sure you're being understood ;-)
>
>But surely if you create an integer object and assign it a value, e.g.
The way you use those words makes me wonder: "assign _it_"?? Which 'it'?
The integer object in this is created from the right hand side of the '='.
Assignment is attaching a name tag (with the name from the left on it) to the
object with sufficient sewing thread to reach a bulletin board where you tack up the tag.
A bare name is tacked on the local bulletin board (name space). The integer object is nameless
in itself. And the name is not the object. The name just leads to the object -- until the lead
is cut and tied to something else (rebound), or the tag is taken off the bulletin board (deleted),
at which time the object may have other names from the same or other bulletin boards (or container
binding sites) attached to it. If there's nothing connected, the object goes in the garbage.

>a = 3,
>why shouldn't Python be able to tell you something like the following:
>name(a)  >>> 'a'
Because there is in general no unique 'a' associated with the object that has already been looked
up and is being passed by reference to name(). What should you get for name(7)? There might be no name.
 
To get an idea, try to write a C routine to do it, being passed a pointer to a memory location
allocated with malloc. What name should it return? How should it find a name?

If you are willing to prefix your names with the name of a special namespace whenever you refer to them,
so it would be name(ns.a) >>> 'a', that could be be done. You can do about anything through descriptor
magic and overriding __get/setattr/ibute__ and spell things in a way that totally abuses
the design intents of Python ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list