Automatically generate Objects from Dcitionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tben88
    New Member
    • Apr 2020
    • 1

    Automatically generate Objects from Dcitionary

    Hi All!

    Programming newbie here. I was looking way to generate instances of a class of my creation from a dictionary (That is in turn created from a tuple and a list, but that part works as I would expect.) Is there a way to do this? So far, what I had is:

    Code:
    key = ("a", "b", "c")
    attribute = [
        ["a", 1, 2, 3],
        ["b", 2, 3, 4],
        ["c", 3, 4, 5]
        ]
    obj = dict(zip(key, attribute))
    
    class Some_object:
        def __init__(self, name, attr1, attr2, attr3):
            self.name = name
            self.attr1 = attr1
            self.attr2 = attr2
            self.attr3 = attr3
    
    for i in obj.keys():
        name = obj[i][0]
        attr1 = obj[i][1]
        attr2 = obj[i][2]
        attr3 = obj[i][3]
        i = Some_object (name,  attr1, attr2, attr3)
    This does seem to create the objects, at least inside the loop, but outside the loop there is no way of calling on them.

    Thanks!
    Last edited by gits; Apr 21 '20, 08:27 PM. Reason: added code tags
Working...