Issue
I want to make a variable with help of a function that gets its name from an argument, so that if I run this;
hi_var(abc)
print(abc)
I will get hi as output
I currently have this, but it is completely not working;
def hi_var(var_name):
text = "hi"
return text as var_name
It would be great if someone knew how to do this
Solution
I am not sure why you want to do this, but if you like you can access the global variable directory and add the variable there:
def hi_var(var_name):
text = 'hi'
globals()[var_name] = text
hi_var('abc')
print(abc)
Answered By - Querenker
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.