Inner Class-
-
- 1) Creating a class inside the other class.
- 2) It help us to avoid the collison because of same class name.
-
Notes-
We can only access the inner class by taking the reference from outer class.
Ex-
class TestClass: def test1(): print("Outer test1") class InnerClass: def test2(): print("Inner test2") class MainClass: def main(): TestClass.test1() TestClass.InnerClass.test2() MainClass.main() Output- Outer test1 Inner test2
– We can access object level members(methods and variables) by using object creation.
Ex-
class OuterClass: def test1(self): print("Outer-test") class InnerClass: def test2(self): print("Inner-test") class TestMain: def main(): obj1 = OuterClass() obj1.test1() obj2 = obj1.InnerClass() obj2.test2() TestMain.main() Output- Outer-test Inner-test
Local Inner Class-
-
- – Defining a class inside the methods.
- – Local inner class will act same as local variable.
- – Local inner class can also be defined inside the another local inner class but it makes the program little bit complex.
-
-
Object Passing Or Message Passing Mechanism-
- – Calling a method by reference of an object as a parameter.
- – Arguments will always be treated as local variable of function.
-
-
-
Notes-
We can also access the outer class functionality from inner class by using object passing mechanism.
-
Nested Function-
– It Is Nothing But A Function Inside Function.
Ex-
def fn_outer(): print("Outer Function") def fn_inner(): print("Inner Function") fn_inner() fn_outer() Output- Outer Function Inner Function