By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,028 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,028 IT Pros & Developers. It's quick & easy.

Automatically generate Objects from Dcitionary

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1. key = ("a", "b", "c")
  2. attribute = [
  3.     ["a", 1, 2, 3],
  4.     ["b", 2, 3, 4],
  5.     ["c", 3, 4, 5]
  6.     ]
  7. obj = dict(zip(key, attribute))
  8.  
  9. class Some_object:
  10.     def __init__(self, name, attr1, attr2, attr3):
  11.         self.name = name
  12.         self.attr1 = attr1
  13.         self.attr2 = attr2
  14.         self.attr3 = attr3
  15.  
  16. for i in obj.keys():
  17.     name = obj[i][0]
  18.     attr1 = obj[i][1]
  19.     attr2 = obj[i][2]
  20.     attr3 = obj[i][3]
  21.     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!
Apr 21 '20 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.