Hello geeks and welcome in this article we will cover Nested classes in Python. As we are aware of the fact that Python is an object-oriented programming language. So a class is a code required to create objects. When a class is instantiated some of the magic methods are called automatically. We have already covered different magic methods in detail.
Let us try to get an overview of the nested class in python. A class defined in another class is known as Nested class. If an object is created using nested class then the object can also be created using the Parent class. Moreover, a parent class can have multiple nested class in it. As we move ahead in this article we will cover everything in detail.
Need of Nested Classes
Let us try to understand why we need Nested Classes. We have humans and clothes, without humans, there is no need for clothes. So we can consider the clothes to be nested class of Humans. With the help of the nested class, we can hide the code as well. Also for a programmer’s point of view, it is easier to correlate classes and enhance understanding.
Creating Nested Classes In Python
In this section, we will primarily focus on creating nested classes. In order to do so let us look at an example.
class language:
def __init__(self):
self.language = 'PYTHON'
self.lg = self.specification()
def show(self):
print("Language:", self.language)
class specification:
def __init__(self):
self.type = 'HIGH-LEVEL'
self.founded = '1991'
def display(self):
print("type:", self.type)
print("Founded:", self.founded)
out = language()
out.show()
ppool = out.lg
ppool.display()
Language: PYTHON
type: HIGH-LEVEL
Founded: 1991
Here above we have successfully created a nested class. Now let us go line by line and understand we have done so. So at first, we have created a class named language. Inside it, we have used the self keyword. The self Keyword is a getaway through which we can access the attribute and methods of our created class. Inside the language class, we have created another nested class called specification. There similarly we have defined the specifications. At last, we get our desired output.
Types of inner classes
The inner class can further be divided into parts that we will be covering next in detail.
1. Multiple Inner Classes
class Employe:
def __init__(self):
self.name = "Employe"
self.intern = self.intern()
self.head = self.head()
def show(self):
print('Employe List')
print('Name:', self.name)
class intern:
def __init__(self):
self.name = 'Smith'
self.Id = '657'
def display(self):
print("Name:", self.name)
print("Id:", self.Id)
class head:
def __init__(self):
self.name = 'Auba'
self.Id = '007'
def display(self):
print("Name:", self.name)
print("Degree:", self.Id)
outer = Employe()
outer.show()
d1 = outer.intern
d2 = outer.head
print()
d1.display()
print()
d2.display()
Output:
Employe List
Name: Employe
Name: Smith
Id: 657
Name: Auba
Degree: 007
A class that has more then one inner class inside it is called Multiple Inner class. Above we have successfully implemented it. Here the Employe class is our outer class. Inside which we have created 2 subclasses. One goes by the name of the Intern and the other by the name of the head.
2. Multilevel inner class
class multi:
def __init__(self):
self.inner = self.Inner()
self.innerinner = self.inner.InnerInner()
def show(self):
print("This is Outer class")
## inner class
class Inner:
def __init__(self):
self.innerinner = self.InnerInner()
def show_classes(self):
print("This is Inner class")
print(self.innerinner)
class InnerInner:
def inner_display(self, msg):
print("InnerInner class")
print(msg)
def inner_display(self, msg):
print("This is Inner class")
print(msg)
outer=multi()
outer.show()
inner = multi.Inner()
innerinner = inner.InnerInner()
innerinner.inner_display("completed")
Output:
This is Outer class
InnerInner class
completed
A multilevel class can be understood as a class that has an inner class. Also, the inner class has an inner class. From the code, you can get an idea of how to execute this particular process. Both types are easy to execute and understand. You can use them as per your need.
Conclusion
In this article, we covered nested classes in Python. We looked at the need for nested classes in programming. Then we also looked at how to implement the nested class through an example. We also looked at the different type of nested classes.
I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read about getpass next.
Can we have a nested static class in the class method?
Yes. Since the methods of a nested class cannot directly access the instance attributes of the outer class. The nested inner class is kind of a static class.
Regards,
Pratik
requires output to be
Thank you for pointing it out. I’ve updated it accordingly.