Can we access Outer class member from an Inner class in Python?

0 votes

Can we access Outer class member from an Inner class in Python?

If yes, then how can we achieve the commented lines?


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"

def display(self):
print("str_in : ", self.__str_in)
print("Inside display() of Inner Class")
#print("x : ", self.__x)
#print("str : ", self.__str)
#self.fn()

obj = Outer()
obj.show()
Feb 12 in Python by Jimmy
1,357 views

1 answer to this question.

0 votes

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()

answered Feb 12 by Omkar
• 67,660 points

Related Questions In Python

0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
98 views
0 votes
1 answer

How can I prevent or alter access to class variables in Python?

The ActiveState solution that Pynt references makes instances of ...READ MORE

answered Dec 5, 2018 in Python by ariaholic
• 7,340 points
73 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
2,273 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
800 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 349 views
0 votes
2 answers

Calling an external command in Python

import os os.system('external_command') Replace external_command in the above code ...READ MORE

answered Oct 16, 2018 in Python by Omkar
• 67,660 points
47 views
0 votes
1 answer

How can I run terminal commands in python?

You can use the subprocess or os module to do this. Using ...READ MORE

answered Feb 3 in Python by Omkar
• 67,660 points
64 views