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 13, 2019 in Python by Jimmy
12,378 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 13, 2019 by Omkar
• 69,210 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
2,034 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,450 points
2,794 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 Apr 7, 2020 in Python by Gitika
• 65,910 points
2,101 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 4, 2018 in Python by charlie_brown
• 7,720 points
12,925 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,060 views
0 votes
1 answer
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,210 points
799 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 4, 2019 in Python by Omkar
• 69,210 points
919 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP