1

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 ?
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Rémi
  • 13
  • 3
  • 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 – Mad Physicist Sep 10 '21 at 21:37
  • OK, I was wondering why I was assuming `Child` inherited from `Parent` :) – chepner Sep 10 '21 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. – martineau Sep 10 '21 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. – Richard Wheeler Sep 17 '21 at 11:28

2 Answers2

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:

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
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
martineau
  • 119,623
  • 25
  • 170
  • 301