print function that prints variable names and their values [duplicate] - python

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?

Related

Execute a string in Python inside a function so that the variables defined can be used [duplicate]

This question already has answers here:
How to get local variables updated, when using the `exec` call?
(3 answers)
Closed 1 year ago.
Given a string (e.g. "lista=[1,2,3]") I would like to be able to use the variable lista.
exec() does the work outside a function, but when used inside a function the variables cannot be used in that same function. I guess it has something to do with local and global variables but I don't really understand the problem.
For example,
def funcion(texto):
exec(texto)
print(lista)
funcion("lista = [3,4,5]")
Gives the error: NameError: name 'lista' is not defined.
add globals
def funcion(texto):
exec(texto, globals())
print(lista)
funcion("lista = [3,4,5]")

Convert string to function name in python [duplicate]

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)

Python shortcut for variable default value to be another variable value if it is None [duplicate]

This question already has answers here:
Is there a way to set a default parameter equal to another parameter value?
(4 answers)
Closed 4 years ago.
I have the following function declaration:
def set_data(obj=None, name='delp', type=None):
Is there a python shortcut, where type is equal with 'name' value(default r passed) if is None ?
The usual idiom is:
def set_data(obj=None, name='delp', type=None):
if type is None:
type = name
(But don't actually use type as a variable name, it'll mask the built in function as described here: Is it safe to use the python word "type" in my code?)

Python variable variables in "main" [duplicate]

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'

is it possible to use a variable to name another variable? [duplicate]

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

Categories

Resources