2

How to access the Parent's name in this example?

class Parent:
    def __init__(self) :
        self.name = "toto"
        self.enfant = self.Child()

    class Child:
        def __init__(self) :
            self.child_name="tata"

        def affiche(self):
            print(?? Parent.name) # how to display the parent's name ?
4
  • 1
    You literally can't. This isn't Java. You're better off defining Child in the global namespace, because it can't see Parent anyway Commented Sep 10, 2021 at 21:37
  • OK, I was wondering why I was assuming Child inherited from Parent :) Commented Sep 10, 2021 at 21:49
  • There's no relationship between the Parent and Child classes except for the fact the the latter class resides in the former's namespace — which is why @Mad Physicist's comment is true. Commented Sep 10, 2021 at 21:54
  • I think your question is hinting at inheritance, and if so your class structure is wrong. With the way you have it structure Child is just another variable of type Child. No different if you had it as a class variable named child: str which is a child of type string. If via inheritance then yes you can get the parents name. Commented Sep 17, 2021 at 11:28

2 Answers 2

2

You can't do it the way you would in Java. Nested classes in Python are completely independent objects. They live without any knowledge of their parent, and in fact, can't see their parent when the class body is being executed.

The solution to your problem is to reference the parent in the child (also, don't bother nesting classes):

class Child:
    def __init__(self, parent):
        self.child_name = "tata"
        self.parent = parent
        
    def affiche(self):
        print(self.parent.name)

class Parent:
    def __init__(self) :
        self.name = "toto"
        self.enfants = [Child()]

If you were to nest Child into Parent, you would still write it the same way, and call the constructor as self.Child(self).


You may want to look at the following questions for more information:

Sign up to request clarification or add additional context in comments.

Comments

0

As I said in a comment, there's no special relationship between your two classes other than the fact the definition of one is nested in the namespace of the other. In order to do what you want, you'll need to explicitly "tell" the Child class who its parent is by passing it as an argument when constructing an instance of one — and it will need to explicitly save that value if it wants to use it later in some other method like affiche().

Here's what I mean:

class Parent:
    def __init__(self) :
        self.name = "toto"
        self.enfant = self.Child(self)  # Pass self as parent argument.

    class Child:
        def __init__(self, parent):
            self.parent = parent  # Remember parent.
            self.child_name="tata"

        def affiche(self):
            print(self.parent.name) # Display the parent's name.


parent = Parent()
parent.enfant.affiche()  # -> toto

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.