amay20
amay20

Reputation: 147

Python creating different objects in current Class

class Family:
    def __init__(self, number_of_family_members):
        self.members = self.create_members(number_of_family_members)

    def create_members(self, number):
        family_people = []
        for i in range(number):
            family_people.append(Human())
           #family_people.append(self.Human())
        return family_people

class Human:
    def __init__(self):
        self.exists = True

I plan on having Family Objects that will contain Human Objects. I am not sure if I am (1) correctly calling the method "create_members" (2) not sure how to initiate Humans

*I am currently learning about Objects so I wasn't sure if this was correct. Thanks!

Upvotes: 0

Views: 47

Answers (1)

jumbopap
jumbopap

Reputation: 4147

What's the issue? Your code is fine. You can inspect it on the terminal to see what is happening. You can also simplify the initialization code.

class Family:
    def __init__(self, number_of_family_members):
        self.members = [Human()] * number_of_family_members

class Human:
    def __init__(self):
        self.exists = True

>>> f = Family(5)
>>> f.members
[<__main__.Human instance at 0x1102ca560>, <__main__.Human instance at 0x1102ca560>, <__main__.Human instance at 0x1102ca560>, <__main__.Human instance at 0x1102ca560>, <__main__.Human instance at 0x1102ca560>]

Upvotes: 1

Related Questions