Answers for "how to name a variable based on string python"

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

Code answers related to "how to name a variable based on string python"

Python Answers by Framework

Browse Popular Code Answers by Language