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, 2019 in Python by Jimmy
2,874 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, 2019 by Omkar
• 69,000 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,760 points
196 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 aryya
• 7,350 points
99 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered 7 hours ago in Python by Gitika
• 26,470 points
22 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,760 points
3,687 views
0 votes
1 answer
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 777 views
0 votes
0 answers
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
• 69,000 points
81 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, 2019 in Python by Omkar
• 69,000 points
100 views