Answers for "python change string to variable name"

4

how to use a string variable as a variable name in python

#works only inside classes
name  = 'varname'
value = 'something'

setattr(self, name, value) #equivalent to: self.varname= 'something'

print (self.varname)
#will print 'something'
Posted by: Guest on October-10-2020
0

how to convert string to function name in python

def add(x,y):
	print('does something')
    
eval("add")(x,y)
Posted by: Guest on August-09-2021

Code answers related to "python change string to variable name"

Python Answers by Framework

Browse Popular Code Answers by Language