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
我计划拥有包含Human Objects的Family Objects。我不确定我是否(1)正确地调用了方法" create_members" (2)不确定如何启动人类
*我目前正在学习对象,所以我不确定这是否正确。谢谢!
答案 0 :(得分:1)
问题是什么?你的代码很好。您可以在终端上检查它以查看发生的情况。您还可以简化初始化代码。
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>]