Creating single variable to consolidate multiple classes

0

I have a dictionary (dict1)

dict1={
    'lala':{
            'name':'lala',
            'lula':0xcafecafe,
            },
    'mene':{
            'name':'mene',
            'lula':0xdeadbeef,
            },}

After that i created a register class to parse in the information

class register:
    def __init__(self,name):
        self.name = dict1[name].get('name')
        self.data = dict1[name].get('lula')

    def self_add(self):
        value = self.data + self.data
        print('self_add value : {}'.format(value))

and create a for loop to populate the it

for name, info in dict1.items():
    reg_class = register(name)
    vars()[reg_class.name]=reg_class ##work
    vars()['base_path'+'.'+reg_class.name]= reg_class ## not working

however, when i use vars()[reg_class.name]=reg_class it is working

>>> mene.data
3735928559
>>> hex(mene.data)
'0xdeadbeef'

but when i use vars()['base_path'+'.'+reg_class.name]= reg_class it is not working

>>> base_path.mene.data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'base_path' is not defined

How can i do that correctly?

python
asked on Stack Overflow May 18, 2021 by Jonathan • edited May 18, 2021 by Jonathan

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0