Answers for "use str var as variable name pytyhon"

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 "use str var as variable name pytyhon"

Python Answers by Framework

Browse Popular Code Answers by Language