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