Dynamic inner classes in python

Previous
Next

Accessing dynamic functionality:

  • Defining a method inside the class using ‘self’ variable become dynamic.
  • We access dynamic members using object reference.
  • We must create object of inner class using outer object address.
  • Inner class reference variable must be of type OuterClass.InnerClass.
  • We access the functionality of inner class as follows
class Outer:
    def m1(self):
        print("Outer-m1")
        return

    class Inner:
        def m2(self):
            print("Inner-m2")
            return

class Access:
    def main():
        obj1 = Outer()
        obj1.m1()

        obj2 = obj1.Inner()
        obj2.m2()
        return

Access.main()

We create object directly to inner class as follows:

class Outer:
    class Inner:
        def fun(self):
            print("Inner-fun")
            return

class Access:
    def main():
        #obj1 = Outer()
        #obj2 = obj1.Inner()
        #obj2.fun()
        Outer().Inner().fun()
        return

Access.main()
Previous
Next

Add Comment

Courses Enquiry Form