This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 5 years ago.
here is what I want to do:
b=1
har1=42
print(f"har{b}")
I want it to display 42
I have tried a few things, but I haven't managed to get it to work
Use either globals() or locals() and construct the key string.
b=1
har1=42
globals()['har'+str(b)]
def f():
b=1
har1=42
print(locals()['har'+str(b)])
f()
Related
This question already has answers here:
What's the scope of a variable initialized in an if statement?
(7 answers)
Scoping in Python 'for' loops
(8 answers)
Closed 1 year ago.
for i in range(1, 10):
a =10
print(a)
It is giving me the output: 10
Expected output: Name is not defined
Can anyone please explain?
This question already has answers here:
Python: Print a variable's name and value?
(13 answers)
Closed 2 years ago.
For debugging I often want to print a variable out. I do this with for example:
print("cat_litter", cat_litter)
That is I print the variable name and its value. Is it possible to define a function to do this for me so I could call something like "printwithname(cat_litter)" for example.
No function needed! Python 3.8+ f-strings support this kind of printing directly:
foo = 1
bar = 2
baz = "Something else"
print(f'{foo=}, {bar=}, {baz=}')
https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging
Python 3.6 -> 3.7 Workaround:
Python: Print a variable's name and value?
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I have many functions with the same prefix. I want to use a combination of strings to invoke the function.
def func_plus_one(v):
return v+1
def func_plus_two(v):
return v+2
a='plus_one'
b='plus_two'
So how can I use 'func_'+a and 'func_'+b to use the function?
If the functions are in the same module as the code needing to reference them, use the globals() of the module. You could call the function indicated by a using:
globals()['func_' + a](x)
If they are in another module, use getattr
getattr(some_module, func_' + a)(x)
This question already has answers here:
Viewing all defined variables
(10 answers)
Closed 3 years ago.
For instance, when I run in a python's top-level program file, I can use __name__
to get the string "__main__", but how do I get the current list of toplevel global variables?
You might want to have a look at this thread: Viewing all defined variables
Quoting the best answer:
dir() will give you the list of in scope variables
globals() will give you a dictionary of global variables
locals() will give you a dictionary of local variables
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I want to automatically define variables using a dynamic string, similar to ${$a} in PHP.
As I do this in a plain script I do not have objects and therefore can not apply setattr or am I wrong?
How do I dynamically define variables?
You can store the variables you want in a dictionary. That's basically what php does, only in Python you can specify which dictionary to use and how to handle it.
You can modify local or global dictionary:
>>> locals()['xxx'] = 'Hi'
>>> xxx
'Hi'
>>> globals()['yyy'] = 'Hello'
>>> yyy
'Hello'