Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

More instances of a same class in one loop. (Python)

Hello. I wrote a class Person in python with three atributes (first_name, last_name, age). I have a big list of dicts witch have a peoples attributes. For example: list = [ { 'first_name' : Adam, 'last_name' : Adamski, 'age' : 30} { 'first_name' : Radek, 'last_name' : Radkowski, 'age' : 22} { 'first_name' : Bartek, 'last_name' : Bartkowski, 'age' : 18} ... ]

Is it possible to make 10 instances of Person class in one loop? I want first instance have a attributes from first dict in a list, second instance have a attributes from second dict in a list and again and again.

3 Answers

Yes.

What kind of issues did you have when you tried?

One option would be to store your instances in a list.

You could try something like this:

class Person:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age


d_list1 = [
    {'first_name': 'Adam', 'last_name': 'Adamski', 'age': 30},
    {'first_name': 'Radek', 'last_name': 'Radkowski', 'age': 22},
    {'first_name': 'Bartek', 'last_name': 'Bartkowski', 'age': 18}
]

person_instances = [Person(entry['first_name'], entry['last_name'], entry['age']) for entry in d_list1]

print(person_instances)

for k in person_instances:
    print('Person instance\'s name is {}, and their age is {}'.format(k.first_name + ' ' + k.last_name, k.age))

#outputs
[<__main__.Person object at 0x7f982c32d630>, <__main__.Person object at 0x7f982c32d6a0>, <__main__.Person object at 0x7f982c32d6d8>]
Person instance's name is Adam Adamski, and their age is 30
Person instance's name is Radek Radkowski, and their age is 22
Person instance's name is Bartek Bartkowski, and their age is 18

Thanks for your answer. Now I have one more question, here we have list of instances, is it possible to make every class in different variable, not in a list. Or if i want to use one instance should I write person_instances[0] for example ?

While you can create dynamic variables it is generally frowned upon. Also, person_instances[0] would work. That's what I did to print the instance property values.

What are you attempting to accomplish?

I thought about extra credit after regular expresion course.

"Write a class to represent a person based on the information in the text file. They should have names, email addresses, phone numbers, jobs, and Twitter accounts. Remember, some of these can be blank, though!

To go ever further, make a class to act as as address book to hold all of the people class instances created above. Can you make it searchable?"

From this text file I have 12 person and thats why i thought about creating all this instances in one loop.