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
152 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 65,850 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,710 points
40 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,320 points
38 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,710 points
957 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
425 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
24 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
• 65,850 points
27 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
• 65,850 points
22 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.