This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 5 years ago.
want to get name of variable issued from outside.
For example:
def process(var):
print name(var)#prints 'nice_name'
nice_name=1
process(nice_name)
Is it even possible in python?
No, as we are just passing a value to the function
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
#Now I would like to somehow get the list printed or returned by only using the string variable.
Use globals or locals:
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
print(globals()[string])
['a','b','c']
If you have multiple list and you want to print them based on some key it's best to use a dictionary.
lstDic = {
"x":['a','b','c'],
"y":['d','e','f'],
"y":['g','h','i']
}
string='x'
print(lstDic[string])
This question already has answers here:
What is getattr() exactly and how do I use it?
(14 answers)
Closed 1 year ago.
how can I use for to run this commands?
self.BTN0.hide()
self.BTN1.hide()
self.BTN2.hide()
self.BTN3.hide()
self.BTN4.hide()
self.BTN5.hide()
self.BTN6.hide()
self.BTN7.hide()
self.BTN8.hide()
self.label.hide()
self.resetBTN.hide()
I cant use this:
layoutList = ['BTN0','BTN1','BTN2','BTN3','BTN4','BTN5','BTN6','BTN7','BTN8','label','resetBTN']
for item in layoutList:
self.item.hide()
how can I use variable value in command in python?
You could try putting the objects directly in the lsit
layoutList = [self.BTN0,self.BTN1,self.BTN2]
for item in layoutList:
item.hide()
This question already has answers here:
How do I create variable variables?
(17 answers)
Dynamically create variable names? [duplicate]
(1 answer)
Closed 2 years ago.
For instance, lets say I have a for loop like this
for i in range(7):
var+i=i
How do I get it to work where 7 different variables are created where var0=0 and var1=1 var2=2, etc..
This question already has answers here:
Determine function name from within that function (without using traceback)
(27 answers)
How to get a function name as a string?
(14 answers)
Closed 3 years ago.
Is there a way to find a functions name from within the function?
def some_function():
function_name = ... # some way of retrieving the name 'some_function'
print(function_name)
The expected value of function_name would be the string: 'some_function'
some_function.__name__ should work
This question already has answers here:
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
Closed 5 years ago.
I couldn't find an answer for this, so can you give a beginner pythonist some help. I have a string and if it is in globals, then I want to use that variable to manipulate it. I get an error that I'm using string to append, when my global variable is a list.
color = []
keyword = "color"
if keyword in globals():
keyword.append("testing")
globals() returns a dictionary, which you can use like any other dictionary
globals()[keyword] = <my_new_value>