Answers for "How to get a variable name as a string in 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 get a variable name as a string in Python"

Python Answers by Framework

Browse Popular Code Answers by Language