create a dict from variables and give name 1

create a dict from variables and give name

mydict = NewClass()
vars = ['a', 'b', 'c']
for v in vars: 
    setattr(mydict, v, eval(v)) 

mydict.__dict__
{'a': 9, 'b': ['hello', 'world'], 'c': (True, False)}

Here is what the above code is Doing:
1. Create a new class called NewClass.
2. Create a new instance of NewClass called mydict.
3. Create a list of variables called vars.
4. Loop through the list of variables.
5. For each variable, set the variable name as an attribute of mydict, and set the variable value as the value of the attribute.
6. Print the __dict__ attribute of mydict.

Similar Posts