How to access outer class from an inner class? How to access outer class from an inner class? python python

How to access outer class from an inner class?


You're trying to access Outer's class instance, from inner class instance. So just use factory-method to build Inner instance and pass Outer instance to it.

class Outer(object):    def createInner(self):        return Outer.Inner(self)    class Inner(object):        def __init__(self, outer_instance):            self.outer_instance = outer_instance            self.outer_instance.somemethod()        def inner_method(self):            self.outer_instance.anothermethod()


The methods of a nested class cannot directly access the instance attributes of the outer class.

Note that it is not necessarily the case that an instance of the outer class exists even when you have created an instance of the inner class.

In fact, it is often recommended against using nested classes, since the nesting does not imply any particular relationship between the inner and outer classes.


maybe I'm mad but this seems very easy indeed - the thing is to make your inner class inside a method of the outer class...

def do_sthg( self ):    ...def messAround( self ):    outerClassSelf = self    class mooble():        def do_sthg_different( self ):            ...            outerClassSelf.do_sthg()

Plus... "self" is only used by convention, so you could do this:

def do_sthg( self ):    ...def messAround( outerClassSelf ):    class mooble():        def do_sthg_different( self ):            ...            outerClassSelf.do_sthg()

It might be objected that you can't then create this inner class from outside the outer class... but this ain't true:

class Bumblebee():    def do_sthg( self ):        print "sthg"    def giveMeAnInnerClass( outerClassSelf ):        class mooble():            def do_sthg_different( self ):                print "something diff\n"                outerClassSelf.do_sthg()        return mooble

then, somewhere miles away:

blob = Bumblebee().giveMeAnInnerClass()()blob.do_sthg_different()    

even push the boat out a bit and extend this inner class (NB to get super() to work you have to change the class signature of mooble to "class mooble( object )"

class InnerBumblebeeWithAddedBounce( Bumblebee().giveMeAnInnerClass() ):    def bounce( self ):        print "bounce"    def do_sthg_different( self ):        super( InnerBumblebeeWithAddedBounce, self ).do_sthg_different()        print "and more different"ibwab = InnerBumblebeeWithAddedBounce()    ibwab.bounce()ibwab.do_sthg_different()

later

mrh1997 raised an interesting point about the non-common inheritance of inner classes delivered using this technique. But it seems that the solution is pretty straightforward:

class Fatty():    def do_sthg( self ):        pass    class InnerFatty( object ):        pass    def giveMeAnInnerFattyClass(self):        class ExtendedInnerFatty( Fatty.InnerFatty ):            pass        return ExtendedInnerFattyfatty1 = Fatty()fatty2 = Fatty()innerFattyClass1 = fatty1.giveMeAnInnerFattyClass()innerFattyClass2 = fatty2.giveMeAnInnerFattyClass()print ( issubclass( innerFattyClass1, Fatty.InnerFatty ))print ( issubclass( innerFattyClass2, Fatty.InnerFatty ))