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'
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'
python use variable name as variable
>>> foo = "bar"
>>> exec(foo + " = 'something else'")
>>> print bar
something else
>>>
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us