Nearby lessons

93 of 108

Python - Inner Classes

Detailed Tutorial Notes

Employee Salary: 20000

In the above application, Employee class members are available to Test class.

Inner classes:

Sometimes we can declare a class inside another class,such type of classes are called inner classes.

Without existing one type of object if there is no chance of existing another type of object,then we

should go for inner classes.

Example: Without existing Car object there is no chance of existing Engine object. Hence Engine

class should be part of Car class.
class Car:
.....
class Engine:
......

Example: Without existing university object there is no chance of existing Department object

class University:
.....
class Department:
......

eg3:

Without existing Human there is no chance of existin Head. Hence Head should be part of Human.

class Human:
class Head:
Note: Without existing outer class object there is no chance of existing inner class object. Hence
inner class object is always associated with outer class object.

Demo Program-1:

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
outer class object creation
inner class object creation
inner class method
Note: The following are various possible syntaxes for calling inner class method

Output

1.

o=Outer()

i=o.Inner()

i.m1()

2.

i=Outer().Inner()

i.m1()

  • Outer().Inner().m1() Demo Program-2:
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Inside a class we can declare any number of inner classes.
class Human:

Output

Name: durga

Dob=10/5/1947

Demo Program-3:

2)

3) def __init__(self):
4) self.name = 'Sunny'
5) self.head = self.Head()
6) self.brain = self.Brain()
7) def display(self):
8) print("Hello..",self.name)

9)

10) class Head:
11) def talk(self):
12) print('Talking...')

13)

14) class Brain:
15) def think(self):
16) print('Thinking...')

17)

18) h=Human()
19) h.display()
20) h.head.talk()
21) h.brain.think()

Output

Hello.. Sunny

Talking...

Thinking...

Garbage Collection:

In old languages like C++, programmer is responsible for both creation and destruction of

objects.Usually programmer taking very much care while creating object, but neglecting

destruction of useless objects. Because of his neglectance, total memory can be filled with useless

objects which creates memory problems and total application will be down with Out of memory

error.

But in Python, We have some assistant which is always running in the background to destroy

useless objects.Because this assistant the chance of failing Python program with memory

problems is very less. This assistant is nothing but Garbage Collector.

Hence the main objective of Garbage Collector is to destroy useless objects.

If an object does not have any reference variable then that object eligible for Garbage Collection.

How to enable and disable Garbage Collector in our program:

By default Gargbage collector is enabled, but we can disable based on our requirement. In this

context we can use the following functions of gc module.

  • gc.isenabled() Returns True if GC enabled

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!Python - Iterators