1

I'm trying to create a Child class within a Person class, but am having trouble accessing the child object after creation, when more than one instance is created dynamically.

For example, I ask the user how many kids they have, and I then want to create an instance of the inner child class for each child they have, and be able to access that information later.

Here is some code that I have tried so far, which works for one child. But I can't figure out how to access the information if they have more than 1 child.

class Person:
  def __init__(self, firstName, lastName, age):
    self.firstName = firstName
    self.lastName = lastName
    self.age = age
    self.numberOfChildren = 0

  class Child:
   def __init__(self, firstName, age):
     self.firstName = firstName
     self.age = age

   def show(self):
     print(self.firstName, self.age)

client = Person("Jake", "Blah", 31)

numberOfChildren = input("How many kids do you have?")
client.numberOfChildren = 2

for i in range(numberOfChildren):
  firstName = input("What is the name of your child?")
  age = input("how old is your child?")
  child = client.Child(firstName, age)
  child.show()

This correctly prints out the child, and seems to create the object within the Person class, but I can not figure out how to access this information through the Person class (client).

If I use something like child.firstName, it obviously only shows the last one entered, as the for loop is over-writing the child object every time. If I knew in advance how many children they would have, I could use something like child1, child2, etc, but since it is dynamic I don't know in advance.

Thanks in advance!

9
  • 3
    Does Child have to be an inner class? This would be much easier if it weren't. Commented Nov 7, 2023 at 20:08
  • 2
    Why is Child a different class at all? What makes a child different from a person? Commented Nov 7, 2023 at 20:09
  • 3
    Person should have a self.children: list variable. Then you should have a Person.add_child() method that appends the child to the list. Commented Nov 7, 2023 at 20:10
  • 1
    I wouldn't design them as inner classes. Depending on what you want to model in the end, I'd maybe just have a single Person model with parents and children lists (or sets). Commented Nov 7, 2023 at 20:15
  • 1
    What you're asking for would be reasonable in Java and other languages, where a Child object would have both self referring to itself and Person.self referring to the enclosing class. That's not how Python works. Inner classes are just separate classes. Except for the fact that you call the inner class Person.Child, it really has no relationship to any particular person unless you specify that yourself. Commented Nov 7, 2023 at 20:16

1 Answer 1

1

I'd design this with a single Person class like

class Person:
    def __init__(self, firstName, lastName, age):
        self.firstName = firstName
        self.lastName = lastName
        self.age = age
        self.children = set()
        self.parents = set()

    def add_child(self, child):
        self.children.add(child)
        child.parents.add(self)

    def __repr__(self):
        return f"Person({self.firstName!r}, {self.lastName!r}, age {self.age}, {len(self.children)} kids)"


client = Person("Jake", "Blah", 31)
child_1 = Person("Bob", "Blah", 1)
child_2 = Person("Penny", "Blah", 2)
client.add_child(child_1)
client.add_child(child_2)
print(client, "- kids:", client.children)
print(child_1, "- parents:", child_1.parents)

This prints out

Person('Jake', 'Blah', age 31, 2 kids) - kids: {Person('Penny', 'Blah', age 2, 0 kids), Person('Bob', 'Blah', age 1, 0 kids)}
Person('Bob', 'Blah', age 1, 0 kids) {Person('Jake', 'Blah', age 31, 2 kids)}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.