Min menu

Pages

Overriding in python

 ConcepttheOverridingin python

In the previous lesson, you saw how the son's class(Subclass)It inherits all the variables and functions of the parent class(Superclass).I also learned how to apply the principle of data hiding in order to ensure that there is no conflict in the names of variables and functions in the child and parent classes.
The principle of data hiding allows us to access the things in the object, whether they originate from the son or the father class.

In the previous lesson, I saw how the Subclass inherits all of the variables and functions found in the Superclass. And I also learned how to apply the principle of data hiding in order to ensure that there is no conflict in the names of the variables and functions in the son and parent classes. The principle of data hiding allows us to access the things in the object, whether they originate from the parent class.


Override: It means defining the function that the son class inherited from the father class again. This new function is similar to the inherited function in terms of form only, that is, it has the same name, type and number of parameters, but its content is different.
So, the real goal oftheOverridingIt is to give the son the opportunity to know the functions according to his needs.

Override: means the definition of the function that the son class inherited from the parent class again. This new function is similar to the inherited function in terms of form only, that is, it has the same name, type and number of parameters, but its content is different. So, the real goal of overriding is to allow the younger class to define functions as needed.


In later lessons, we will inherit from ready-made classes in Python, and doOverridefor the functions in it to fit the applications we are going to build.

In later lessons, we will inherit from ready-made classes in Python, and do Override for the functions in it to suit the applications we will build.

 Python overriding what are the terms theOverriding

  • You can't doOverrideA function is basically cryptically defined because masking the names ensures that there is no problem with name collisions.

  • The number and type of parameters of the new function must match the number and type of parameters of the old function.

The failure to fulfill these two conditions means that you know a new function in the son class, and the function inherited from the father class, the object you create from the son class can invoke it.



Example

Now suppose we define a class namedCountryInfo,It contains a function named .print_language().
Then we defined three classes, and they all inherit from the classesCountryInfo.So each of them will contain the functionprint_language().
Here the idea is that any class that inherits from a class CountryInfomay have to redefine the function print_language()in order for it to fit.

Example

CountryInfo.py
# This class is considered the basic class for any country in the world, so any class representing a country must inherit it
		  class CountryInfo:
	  
		  def print_language(self):
		  print('English') # Here we have set English as the default language for all countries
	

Australia.py
# So that we can inherit from it PrintInfo.py which is in the file PrintInfo here we have included the class
		  from CountryInfo import CountryInfo
	  
	  
		  # 'English' which prints the word print_language() and thus inherits the CountryInfo function from the Australia class. Here we have defined a class named
		  class Australia(CountryInfo):
		  pass
	  
		  # Again because English is Australia's print_language() here there is no need to define the function
	

Lebanon.py
# So that we can inherit from it PrintInfo.py which is in the file PrintInfo here we have included the class
		  from CountryInfo import CountryInfo
	  
	  
		  # 'English' which prints the word print_language() and thus inherits the function CountryInfo inherits from the Lebanon class Here we have defined a class named
		  class Lebanon(CountryInfo):
	  
		  # Because the Arabic language is the language of Lebanon 'English' instead of the word 'Arabic' again to make it print the word print_language() here the function must be defined
		  def print_language(self):
		  print('Arabic')
	

Spain.py
# So that we can inherit from it PrintInfo.py which is in the file PrintInfo here we have included the class
		  from CountryInfo import CountryInfo
	  
	  
		  # 'English' which prints the word print_language() and thus inherits the CountryInfo function from the Spain class. Here we have defined a class named
		  class Spain(CountryInfo):
	  
		  # Because the Spanish language is the language of Spain 'English' instead of the word 'Spanish' again to make it print the word print_language() here the function must be defined
		  def print_language(self):
		  print('Spanish')
	

Test.py
# So that we can create objects including Spain, Lebanon, Australia, here we have included the classes
		  from Australia import Australia
		  from Lebanon import Lebanon
		  from Spain import Spain
	  
	  
		  # Spain, Lebanon, Australia Here we have created objects from the three countries
		  au = Australia()
		  lb = Lebanon()
		  sp = Spain()
	  
		  # From each object to display each country's language print_language() here we called the function
		  au.print_language()
		  lb.print_language()
		  sp.print_language()
	

We will get the following result when we run the fileTest.

English
Arabic
Spanish

If you understood the previous example well, now you definitely understand what you might need to doOverrideFor dyslexia in Klass Jr.


Technical information

If you go back to classLebanon,You'll find we didOverrideThe function has print_language()to print a word Arabicinstead of a word Englishif an object is created from the class Lebanonand called from it.

Actually, the class Lebanonnow has two functions that have their name print_language()and not one function as follows:

  • The first is the one he basically inherited from the classCountryInfo.

  • The second is the one we defined in it again in the classLebanon (i.e. when we didOverride).

Since the class Lebanonhas two functions with the same name and number of parameters, how did the Python interpreter know which function to execute when the code was run?

The Python interpreter searches first for the function you called in the class from which the object was created.
If it finds the function in the class, it will execute it. If he does not find it, he searches for it in the father's class. If he finds it in the class, the father implements it.


note

In the class, Lebanonyou can call the function print_language()that it inherited from the class CountryInfo, depending on its namesuper().
Don't worry if you don't understand what it means now because you'll get to know this function shortly.

Python function super()in Python

This function makes you able to access the function defined in the parent class(Superclass)From the son's class(Subclass)Whether there is a conflict in the names or not, and thus the son class becomes able to rewrite the function that he inherited from the father class and reach the function that he inherited from the father class as well.

In other words, this function is to make the programmer able to take advantage of the existing function in the father class and add new things to it in the son class.
From the following example, you will learn how to __init__()automatically call the function in the parent class from the function __init__()in the child class.

Python The super() function in Python This function makes you able to access the function defined in the parent class (Superclass) from the subclass, whether there is a name conflict or not, and thus the son class becomes able to rewrite the function that he inherited from the parent class and reach the function which he inherited from the classe the father also. In other words, this function makes the programmer able to take advantage of the function in the parent class and add new things to it in the son class. From the following example, you will learn how to automatically call the function __init __ () in the parent class from the function __init __ () in the son class.

to remember

Parameters that you define inside the function __init__()are converted into properties of the object being created and this function is called automatically when the object is created.

remember Parameters that you define within the __init __ () function are converted into properties of the object being created. This function is called automatically when the object is created.

Example

Now suppose we define a class namedPerson,It contains two features( nameand age)They are created at the moment an object is created from the class because we put them in the function__init__().

Then we defined a class whose name Studentinherits from the classPerson.In this class, we have defined the function __init__()anew and put in it the same parameters as the function __init__()that the class inherited, and we added a new parameter namedspecialization.
We have also defined a function called print_info()that displays all property values ​​in the class.

Example Now suppose we define a class called Person, it contains two properties (name and age) that are created at the moment of creating an object from the class because we put them in the __init__() function. Then we define a class called Student inherits from the Person class. In this class we have defined the function __init __ () again and put the same parameters in it as in the function __init __ () that the class inherited, and added a new parameter called specialization in it. We have also defined a function called print_info() that displays all property values ​​in the class.

Example

Person.py
# This class is considered the basic class for any human being, as every person has a name and age
		  class Person:
	  
		  # which will be owned by any object we create from Person in which we set the class properties __init__() here we have defined the function
		  def __init__(self, name, age):
		  self.name = name
		  self.age = age
	

Student.py
# So that we can inherit from it Person.py which is in the Person file here we have included the class
		  from Person import Person
	  
	  
		  # Because Person can use it as the main class for creating any student, this class is inherited from the Student class. Here we have defined a class named
		  class Student(Person):
	  
		  # which will be owned by any object we create from it Student in which we put the properties of the class __init__() here we have defined the function
		  def __init__(self, name, age, specialization):
		  # which we enter when creating the age object and the parameter value of the name parameter and we pass it the value of the parameter Person in the __init__() class here we called the function
		  super().__init__(name, age)
		  The # that we mentioned in this class will be given the last value we pass in the parentheses of the object we create from this class's specialization property
		  self.specialization = specialization
	  
		  # Student Here we have defined a function that prints all property values ​​owned by any object we create from the class
		  def print_info(self):
		  print('name:', self.name)
		  print('age:', self.age)
		  print('specialization:', self.specialization)
	

Test.py
# So that we can create an object from Student.py in the Student file here we have included the class
		  from Student import Student
	  
		  # s and store it in Student Here we create an object from the class
		  s = Student('Mhamad', 24, 'Computer Science')
	  
		  # So it displays the values ​​of its s properties from the print_info() object here we called the function
		  s.print_info()
	

We will get the following result when we run the fileTest.

name: Mahamad
age: 24
specialization: Computer Science