An inner class or nested class is a defined entirely within the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have more than one inner classes, but in general inner classes are avoided.
Python is a great programming language that supports OOP. You will use it to define a class with attributes and methods, which you will then call. Python offers a number of benefits compared to other programming languages like Java, C or R. It's a dynamic language, with high-level data types. An inner class or nested class is a defined entirely within the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have more than one inner classes, but in general inner classes are avoided. Related Course: Python Programming Bootcamp: Go from zero to hero. Inner class example. As noted already in the answers, data classes from dataclasses cannot generate slots for the simple reason that slots must be defined before a class is created. In fact, the PEP for data classes explicitly mentions this: At least for the initial release, slots will not be supported. slots needs to be added at class creation time.
Related Course:
Python Programming Bootcamp: Go from zero to hero
Python Programming Bootcamp: Go from zero to hero
Inner class example
We create a class (Human) with one inner class (Head).
An instance is created that calls a method in the inner class:
Output:
In the program above we have the inner class Head() which has its own method. An inner class can have both methods and variables. In this example the constructor of the class Human (init) creates a new head object.
Multiple inner classes
Python Slots Vs Data Classification
You are by no means limited to the number of inner classes, for example this code will work too:By using inner classes you can make your code even more object orientated. A single object can hold several sub objects. We can use them to add more structure to our programs.
If you are new to Python programming, I highly recommend this book.
Python Inheritance
Inheritance allows us to define a class that inherits all the methods and properties from another class.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class.
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other class:
Example
Create a class named
Person
, with firstname
and lastname
properties, and a printname
method: class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person('John', 'Doe')
x.printname()
Try it Yourself »def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person('John', 'Doe')
x.printname()
Create a Child Class
To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:
Example
Create a class named
Student
, which will inherit the properties and methods from the Person
class:Note: Use the
pass
keyword when you do not want to add any other properties or methods to the class.Now the Student class has the same properties and methods as the Person class.
Example
Use the
Try it Yourself »Student
class to create an object, and then execute the printname
method:Add the __init__() Function
So far we have created a child class that inherits the properties and methods from its parent.
We want to add the
__init__()
function to the child class (instead of the pass
keyword).Note: The
__init__()
function is called automatically every time the class is being used to create a new object.Example
Add the
__init__()
function to the Student
class: class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
def __init__(self, fname, lname):
#add properties etc.
When you add the
__init__()
function, the child class will no longer inherit the parent's __init__()
function.Note: The child's
__init__()
function overrides the inheritance of the parent's __init__()
function.To keep the inheritance of the parent's
__init__()
function, add a call to the parent's __init__()
function:Example
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Try it Yourself »def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the
__init__()
function.Use the super() Function
Python also has a
super()
function that will make the child class inherit all the methods and properties from its parent:Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
Try it Yourself »def __init__(self, fname, lname):
super().__init__(fname, lname)
By using the
super()
function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.Add Properties
Example
Add a property called
graduationyear
to the Student
class: class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019
Try it Yourself »def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019
In the example below, the year
2019
should be a variable, and passed into the Student
class when creating student objects. To do so, add another parameter in the __init__() function:Python Slot Machine Code
Example
Add a
year
parameter, and pass the correct year when creating objects: class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student('Mike', 'Olsen', 2019)
Try it Yourself »def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student('Mike', 'Olsen', 2019)
Add Methods
Example
Python Slots Vs Data Classic
Add a method called
welcome
to the Student
class: class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print('Welcome', self.firstname, self.lastname, 'to the class of', self.graduationyear)
Try it Yourself »def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print('Welcome', self.firstname, self.lastname, 'to the class of', self.graduationyear)
If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.