0
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!

5
  • 5
    Can you explain more about what you are going to do? and what's wrong with your code? Commented Dec 24, 2015 at 20:30
  • Of the 2 lines in create_members family_people.append(Human()) this is the correct way to do it. Commented Dec 24, 2015 at 20:35
  • 1
    The commented line won't work since you don't have a Human() method in the Family class. Commented Dec 24, 2015 at 20:37
  • Your code looks fine to me as well, the suggestion is that if you are going to call the create_members function on init and you won't need it as a separate method, you can just embed the current code in the init function Commented Dec 24, 2015 at 20:38
  • thank you for your help! Commented Dec 24, 2015 at 21:21

1 Answer 1

1

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>]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.