Printing a variable's name not its value

T

travislspencer

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.
 
T

travislspencer

Bruno said:
(e-mail address removed) a écrit :

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

Oh, woops. I'll fix that.
def printVerbose(*args, **kwargs):
if VERBOSE in globals:
for a in args:
if a in globals:
value = globals[a]

for k, v in kwargs:
print "%s: %s" % (k, v)

Perfect. Thanks for the pointer.
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.

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

Bruno Desthuilliers

(e-mail address removed) a écrit :
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 = {}

globals() is a builtin function, you should no shadow it.
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)

-> printVerbose(HOME_DIR, SHELL, someString=someString)
def printVerbose(*args):
def printVerbose(*args, **kwargs):
if VERBOSE in globals:
for a in args:
if a in globals:
value = globals[a]

for k, v in kwargs:
print "%s: %s" % (k, v)

(snip)

I've been told on #python that there isn't a way to get a variable's
name. I hope this isn't so.

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.
 
T

tiissa

Simon said:
as you have been told, there is no way to get a variable's name

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.
 

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
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top