#πŸ”’ print reliable the name of a list of variables from a function

16 messages Β· Page 1 of 1 (latest)

spice surge
#

Hello,
I want to create a function that takes a list of variables and prints one by one 'the value of X is xx' for example
something like


def printVars(listOfVariables,listOfVariablesNames):
  for n,var in enumerate(listOfVariables):
    print('the variable '+listOfVariablesNames[n]+' is equal to '+str(var))
U=1
n=3
T=2
printVars([U,n,T],['U','n','T'])

this exactly behavior but a function which will not have the second list so that I will call simply printVars([U,n,T])
sorry if i am not clear enought... not sure how to explain it better. I do this in bash/shell scripts without issues but not sure how to achieve this in python.

sacred quiverBOT
#

@spice surge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

primal viper
#

oh wait that won't work because the loop will remap the name

#

ideally if you care about the actual name of something, you should be using a dictionary

limpid kiln
#

values do not know what their variable names are, so the idea is fundamentally impossible

as fashoomp said you need to supply the names in some from, dicts are ideal

spice surge
#

what I thought..... in bash I could achieve this, but it is a different approach to python, thanks for the clarification that it is not possible πŸ™‚

limpid kiln
# primal viper you can use the `=` format specifier

this is actually a potentially good idea... if you use that format specifier when passing arguments, you only need to refer to the variable once to pass both name and (stringed) value

of course it makes the function weird and hard to use, but it'd work

spice surge
#

what do you mean?
using something like this?

def printVars(U='',n='',T=''):
limpid kiln
#

no, if you did that you'd need to create a new function every time you had a different variable to print...

the = format specifier is used in f-strings, the idea is very hacky, i'll whip up an example

#

!e ```py
def print_vars(names_values):
for name_value in names_values:
name, value = name_value.split('=')
print(f'The variable {name} is equal to {value}')

U = 1
n = 3
T = 2
print_vars( [
f'{U=}',
f'{n=}',
f'{T=}',
] )```

sacred quiverBOT
unreal flame
#

!e ```py
U, n, T = 10, "hello world", [1, 2, 3]
print(f"{U=} {n=} {T=}")

sacred quiverBOT
sacred quiverBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.