For example, I want to write a configuration file in JSON, read it with dict, and make it a variable of Class. String lookups can be buggy when typoing.
For the time being, if it is setting data (e.g. numerical value, character string), you can go by updating __dict__
.
class Conf:
def __init__(self):
pass
d = { 'a': 3 }
c = Conf()
c.__dict__.update(d)
print(c.a)
Conversely, you can also create a dict from a variable by iterating __dict__
.
TODO
Recommended Posts