A Python is an Object-Oriented Programming Language, everything in python is related to objects, methods, and properties. A class is a user-defined blueprint or a prototype, which we can use to create the objects of a class. The class is defined by using the class keyword.
Example of class –
First of all, we have to understand the __init__() built-in method for understanding the meaning of classes. Whenever the class is being initiated, a method namely __init__() is always executed. An __init__() method is used to assign the values to object properties or to perform the other method that is required to complete when the object is created.
Example of class with __init__() method –
Course : Campus Preparation Duration : As per your schedule
Inner Class in Python
A class defined in another class is known as inner class or nested class. If an object is created using child class means inner class then the object can also be used by parent class or root class. A parent class can have one or more inner class but generally inner classes are avoided.
We can make our code even more object oriented by using inner class. A single object of the class can hold multiple sub-objects. We can use multiple sub-objects to give a good structure to our program.
First we create a class and then the constructor of the class.
After creating a class, we will create another class within that class, the class inside another class will be called as inner class.
Green Name:Green Light Green 023gfd Name: Light Green Code: 023gfd
Why inner class?
For the grouping of two or more classes. Suppose we have two classes remote and battery. Every remote needs a battery but battery without remote won’t be used. So, we make the Battery an inner class to the Remote. It helps us to save code.
With the help of the inner class or nested class, we can hide the inner class from the outside world. Hence, Hiding the code is another good feature of the inner class.
By using the inner class, we can easily understand the classes because the classes are closely related. We do not need to search for classes in the whole code, they all are almost together.
Though inner or nested classes are not used widely in Python it will be a better feature to implement code because it is straight forward to organize when we use inner class or nested class.
# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()
Types of inner classes –
- Multiple inner class
- Multilevel inner class
Multiple inner class
The class contains one or more inner classes is known as multiple inner class. We can have multiple inner class in a class, it is easy to implement multiple inner class.
Example of multiple inner class –
In outer class Name: Doctor In inner class 1 Name: Dr. Savita Degree: BDS In inner class 2 Name: Dr. Amit Degree: DM
Multilevel inner class
The class contains inner class and that inner class again contains another inner class, this hierarchy is known as multilevel inner class.
Example of multilevel inner class –
This is an outer class
This is an inner class
This is an inner class of inner class means multilevel inner class
- Inheritance in Python Inner Class
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Python program to create Bankaccount class with deposit, withdraw function
- Python | Avoiding class data shared among the instances
- Python | Using variable outside and inside the class and method
- self in Python class
- How to create a list of object in Python class
- How to create an empty class in Python?
- Print objects of a class in Python
- Python: Call Parent class method
- Overriding Nested Class members in Python
- How to Get a List of Class Attributes in Python?
- Python - Access Parent Class Attribute
- Extend Class Method in Python
- Customize your Python class with Magic or Dunder methods
- Abstract Base Class (abc) in Python
- Creating Decorator inside a class in Python
- How to Change a Dictionary Into a Class?
- Different ways to access Instance Variable in Python
- Dunder or magic methods in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.