If you want to access outer class variables and methods then you will have to create instances of the outer class in inner class as well, Please go through the below we have made few changes in your code based on if variables are private or public,
In variables are Private
class Outer:
def __init__(self):
self.__x = 20
self.__str = "Outer Class"
def show(self):
self.i1 = self.Inner()
self.i1.display()
def fn(self):
print("Hello from fn() of Outer Class")
print("str : ", self.__str)
class Inner:
def __init__(self):
self.__str_in = "Inner Class"
self.outer=Outer()
def display(self):
print("str_in : ", self.__str_in)
print("Inside display() of Inner Class")
print("x : ", self.outer._Outer__x)
print("str : ", self.outer._Outer__str)
self.outer.fn()
obj = Outer()
obj.show()
If variables are Public
class Outer:
def __init__(self):
self.x = 20
self.str = "Outer Class"
def show(self):
self.i1 = self.Inner()
self.i1.display()
def fn(self):
print("Hello from fn() of Outer Class")
print("str : ", self.str)
class Inner:
def __init__(self):
self.__str_in = "Inner Class"
self.outer=Outer()
def display(self):
print("str_in : ", self.__str_in)
print("Inside display() of Inner Class")
print("x : ", self.outer.x)
print("str : ", self.outer.str)
self.outer.fn()
obj = Outer()
obj.show()