use variable value in command python [duplicate] - python

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()

Related

How to look for a variable named the same as a string in Python [duplicate]

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])

Python: one variable but different values? [duplicate]

This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
What does __all__ mean in Python?
(10 answers)
Closed 5 years ago.
I have a line of code from Python forbidden fruit module:
__all__ = 'curse', 'curses', 'reverse'
I know what strings are and I know what arrays and tuples are. What kind of variable is this? How can we use this and for what?
It's a tuple. If you want to find out the type of something, use the type function - e.g.
type(__all__)

if string contains global variable, how to use that global variable? [duplicate]

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>

Get local name of variable issued to function [duplicate]

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

set a key without value for a dictionary in python [duplicate]

This question already has answers here:
Python function global variables? [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to set a dictionary for the staff's salary in Python. I want to create two functions, getname() and getsalary() so I tried:
empDic={}
def setname(n):
empDic={'n':''}
And then in python interactive 2.7 I typed:
>>>setname('marson')
>>>print empDic
{}
The result is still an empty dictionary, How can I deal with that?
You would need to modify the original dict:
empDic={}
def setname(d, n):
d[n] = ""
setname(empDic,"marson")
print(empDic)
{'marson': ''}
You are creating a new local variable empDic in your function, you are also using a string "n" not the name n passed in to the function.

Categories

Resources