This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 1 year ago.
How to pass a variable for an f-string '=' debugging operator?
from datetime import datetime
def print_var(var):
print(str(datetime.now())[:19], end=' ')
print(f'{var = }')
test = 5
print_var(test)
I expect print_var(test) to print the variable name from outside the function, ie.
test = 5
Please refer to Python: Print a variable's name and value? for the context of the question.
In f-strings you need to put the variable that needs to be printed between accolades. Like this:
from datetime import datetime
def print_var(var):
print(str(datetime.now())[:19], end=' ')
print(f'var = {var}')
Running the test yields the following:
test = 5
print_var(test)
>>>2021-10-06 11:32:05 var = 5
Related
This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 2 years ago.
I have the following code:
var1='aaa'
var2='bbb'
debug(var1, var2)
I would like the debug function to print:
"var1=aaa var2=bbb"
My function currently looks like
def debug(thing1, thing2):
print(f"{thing1=} {thing2=}")
and outputs:
"thing1=aaa thing2=bbb"
I tried using locals() in the function as well but got the same output. I would like to have "var1" and "var2" in the output without hardcoding them in the print statement.
You need to come at this problem in a slightly different way.
As stated, there is no way for debug to know the variable names.
You need to supply that information:
>>> import pprint
>>> var1 = 'aaa'
>>> var2 = 'bbb'
>>> def debug(**kwargs):
... pprint.pprint(kwargs, width=20)
...
>>> debug(var1=var1, var2=var2)
{'var1': 'aaa',
'var2': 'bbb'}
This question already has answers here:
Calling variable defined inside one function from another function
(6 answers)
Closed 2 years ago.
I have open source web server written in Python. Inside the server I have a wrong_checker() function which takes a value and checks it against real_value. I can access this function.
def wrong_checker(value_of_check):
return (value_of_check == "" or (not value_of_check and real_value)) \
or value_of_check != real_value
Now I want to write function find_value() to find and print the value of real_value.
def find_value():
???????
Well, normally you should write a comparer funciton
def compare(value_a, value_b):
#compare stuff and return 1, 0 or -1
#1 for value_a greater, 0 for equal and -1 for value_b greater
Then you are more flexible.
This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
How can I concatenate str and int objects?
(1 answer)
Closed 6 months ago.
So, I want this code to have user input Dogechain address, and I get the balance of that address. When I execute this I get error:
url = "https://dogechain.info/api/v1/address/balance/"+ a
Error:
TypeError: cannot concatenate 'str' and 'int' objects
Code:
import requests
def main():
a = input("Address?")
url = "https://dogechain.info/api/v1/address/balance/"+ a
response = requests.get(url)
bal = response.json()[0]["balance"]
print("Balance:",bal)
main()
url = "https://dogechain.info/api/v1/address/balance/" + str(a)
or
url = "https://dogechain.info/api/v1/address/balance/{}".format(a)
And #Ryan O'Donnell's comment below explains why, thanks.
This question already has answers here:
Global Variable in Python
(2 answers)
Closed 5 years ago.
I have a little python application, and declared at the top of the application a string called lastMsg, in a function, this string should be changed, but instead of changing the existing string, it creates a new string, how can I change the old string?
If I guessed correctly what you are trying to do (do share some code to further explain your answer):
You need to use the global keyword to specify you want to change the global variable.
myVar = "1"
def myFun():
global myVar
myVar = "2"
print(myVar)
myFun()
print(myVar)
Should print:
1
2
It's not quite clear what you're asking.
I think you mean this:
lastMsg = "some string"
def a_function():
lastMsg = "new value"
If so, you can change it using the global keyword:
lastMsg = "some string"
def a_function():
global lastMsg
lastMsg = "new value"
This question already has answers here:
How to create an unknown amount of variables in python
(6 answers)
Closed 9 years ago.
after much googling i now ask. Is there a way to append an integer to the end of a variable name. Essentially creating a new variable with each iteration of a for loop. IE:
def parser(lst):
count = 0
for obj in lst:
if isinstance(obj,list):
parser(obj)
else:
for string in obj:
var+count = o
This is what i get when i try to run above code:
SyntaxError: can't assign to operator
You almost certainly want to use a list:
def parser(lst):
vars = []
for obj in data: # did you mean "for obj in lst:"?
if isinstance(obj,list):
parser(obj)
else:
for string in obj:
vars.append(o) # did you mean "vars.append(string)"?
Then, instead of, say, var5, you would use vars[5].
Doorknob of Snow's answer is correct, but for completeness, you can create a new variable using locals()[var + str(count)] = o. But this is a bad idea, so don't.