Well, wrap your posts in [ code = Python ] [/code] tags for greater clarity.
My favorite way to do this would be to pull the information out of tuples like this:
class person():
def __init__(self):
self.name = ' '
self.job = ' '
self.age = 0
people = [('abc','worker',20),
('efg', 'student',22),
('hlk', 'doctor', 25)] # yeah, right!
personList = []
for name,job,age in people: # this unpacks each tuple in the list.
tmp = person()
tmp.name = name
tmp.job = job
tmp.age = age
personList.append(tmp)
Now, if you're willing to modify the __init__ a bit, then you can write even cleaner code:
class person():
def __init__(self,name='',job='',age=0):
self.name = name
self.job = job
self.age = age
people = [('abc','worker',20),
('efg', 'student',22),
('hlk', 'doctor', 25)] # yeah, right!
personList = []
for name,job,age in people:
personList.append(Person(name,job,age))
Jeff
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156