Printing a variable's name not its value

Printing a variable's name not its value

Post by travislspe » Fri, 22 Jul 2005 03:47:48


Hey,

I am trying to write a function that takes an arbitrary number of
arguments and does one of two things. If the variable is a key in a
dictionary, it prints the key and its value. Otherwise, if any of the
variables isn't in the dictionary, the function prints the variable's
name and value.

Here is what I have so far:

globals = {}
HOME_DIR = "The user's home directory"
SHELL = "The user's shell"

def someFunction():
someString = "This is a test"
globals[VERBOSE] = True
globals[HOME_DIR] = os.getenv("HOME")
globals[SHELL] = os.getenv("SHELL")

printVerbose(someString, HOME_DIR, SHELL)

def printVerbose(*args):
if VERBOSE in globals:
for a in args:
value = ""

if a in globals:
value = globals[a]
# else
# a = a.__name__
# value = a

print "%s: %s" % (a, value)

This prints something like this:

The user's home directory: /home/tspencer
The use's shell: zsh

But what I would like it to print is this:

The user's home directory: /home/tspencer
The use's shell: zsh
someString: This is a test

I've been told on #python that there isn't a way to get a variable's
name. I hope this isn't so. If it helps, I am trying to do something
like what the C preprocessor's # operator does.

TIA.

Regards,

Travis Spencer
 
 
 

Printing a variable's name not its value

Post by Simon Dahl » Fri, 22 Jul 2005 04:12:51

as you have been told, there is no way to get a variable's name, take a
look at http://www.yqcomputer.com/
is so.

 
 
 

Printing a variable's name not its value

Post by travislspe » Fri, 22 Jul 2005 05:01:32


Oh, woops. I'll fix that.


Perfect. Thanks for the pointer.


OK. This seems like it might take some getting used to. Thanks again
for the help.


Regards,

Travis Spencer
 
 
 

Printing a variable's name not its value

Post by travislspe » Fri, 22 Jul 2005 05:02:29


Thanks, Simon, for the link.

Regards,

Travis Spencer
 
 
 

Printing a variable's name not its value

Post by Bruno Dest » Fri, 22 Jul 2005 05:19:06


XXXX@XXXXX.COM a rit :

globals() is a builtin function, you should no shadow it.


-> printVerbose(HOME_DIR, SHELL, someString=someString)

def printVerbose(*args, **kwargs):


for k, v in kwargs:

(snip)


It is so. In fact, there is nothing like a 'variable' in Python. What we
have are names bound to objects. Names 'knows' what objects are bound to
them, but objects knows *nothing* about names they are bound to.
 
 
 

Printing a variable's name not its value

Post by tiiss » Fri, 22 Jul 2005 05:39:30


Well, if you really want to, you can get all the names bound to a given
object:


def get_names(obj):
g = globals()
names = []
for name in g:
if g[name] is obj:
names.append(name)
return names


Then you can play around:

>>> list1 = []
>>> list2 = [list1]
>>> get_names(list2)
['list2']
>>> list3 = list2
>>> get_names(list2)
['list3', 'list2']
>>> get_names(1)
[]
>>> a = 1
>>> get_names(1)
['a']
>>> b = 1
>>> get_names(1)
['a', 'b']
>>> get_names(a)
['a', 'b']
>>> c = 4/3.
>>> d = 4/3.
>>> get_names(c)
['c']
>>> get_names(d)
['d']
>>> get_names(4/3.)
[]
>>>


But I wouldn't do it. If I want a name to be attached to some objects, I
usually include it as a member/property of my class.
 
 
 

Printing a variable's name not its value

Post by travislspe » Fri, 22 Jul 2005 05:56:11


for k, v in kwargs.iteritems():

Well, almost perfect ;)

Regards,

Travis Spencer