Hello. ^^
I was making a program "Self the string delivered as a factor.Can we replace it with a variable?" I'm asking this question because I'm
For example, this is the case.
def a(self, string) :
self.string = {
~~~~~~~~
}
a('hello')
It's a very omitted content, but the intention is whether you can variable the argument you delivered.
After passing the string 'hello' to the string with a('hello'), insert it into the string position Finally, can you name the variable self.hello?
I wonder if this is possible inside Python.
Thank you all for your interest in the question, and I look forward to hearing from you soon. Thank you ^^
python string variable
There's a way to get rid of it, but don't make such a strong move.
It's a question that's already been adopted, but the answer seems strange, so I'm writing.
You can use setattr
.
class Dynamic:
def set_attribute(self, string):
setattr(self, string, {"hello": "world"})
d = Dynamic()
d.set_attribute("hello")
print(d.hello) # {'hello': 'world'}
d.set_attribute("world")
print(d.world) # {'hello': 'world'}
© 2024 OneMinuteCode. All rights reserved.