Python Inheritance
The support allows us to define a class with all the parental involvement activities that allow us to add more. In this article, you will learn how to use the Python heritage.
Any language in which the program is eligible is not to see or do it if it does not support the inheritance. Of course, Python supports the inheritance, which also supports many inheritances.
Classes can inherit other classes. The class may inherit the characteristics and methods of the ordinary class, called superclass. A class of superclass is called, also known as class or classroom class. The bedroom is also called a grandpa. There is no connection between classes. Such a relationship or a variety of things to know about real life. Think of the car, for example. Motorcycles, trucks, buses, and cars are a car. Vehicles, cars, motorcycles, cars, and trucks are carriers and cars all car. We can apply for Python classroom cars, which may be in the form of speed and breaks. Vehicles, buses and cars and bicycles can be implemented as the parts below will inherit the transport system.
Table of Contents:
1) What is inheritance?
--> Python inheritance syntax
--> Example of Inheritance in python
2) Method overriding in python
What is the inheritance?
The support is a powerful force in a targeted product.
This refers to the definition of a new class that has little effect on the classroom. A new class is called for (or children), for example, in the classroom (or parent).
the classroom is inherited by classroom nature, adding new features. This has resulted in re-use of the code.
Python inheritance syntax
To show the inheritance, let's take a sample.
class BaseClass: Body of base class class DerivedClass(BaseClass): Body of derived class
A member is a closed picture 3 or more. For example, we have a Polygondefined class.
Types of such information to store the number, n size of each side of the list, hand.
class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides
self.sides = [0 for i in range(no_of_sides)]
def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
Example of Inheritance in python
InputSides takes the scope of each of the components, computers () shown to be true.
One triangle is a triangle. Therefore, we can create a class called Triangle which is between the stream. This makes all the features included in a radical class that are easy to find in the triangle. We do not need to define again (recycle code). The triangle is defined as follows.
class Triangle(Polygon):
def __init__(self):
Polygon.__init__(self,3)
def findArea(self):
a, b, c = self.sides
# calculate the semi-perimeter
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
However, the third grade receives a new method () to obtain and print the triangle. Here are examples of killings.
>>> t = Triangle()
>>> t.inputSides()
Enter side 1 : 3
Enter side 2 : 5
Enter side 3 : 4
>>> t.dispSides()
Side 1 is 3.0
Side 2 is 5.0
Side 3 is 4.0
>>> t.findArea()
The area of the triangle is 6.00
We can see that even though we do not decide how to use this type of SIDA () or payment () for the triangle, we can do it.
If no features are found in the classroom, the search continues in the elementary school. This is repeated over time if the primary class is taken from another class.
Method overriding in python
In the previous example, let the __init () method describe the two as well as the triangle graph. When this happens, the classroom settings in the classroom are more difficult than the language. In that way, the __init __ () triangle receives the same density.
Often, when you change the basic method, you seem to extend the meaning you can easily convert. The same way is done by calling the classroom in the middle of the classroom (call the call .__ init __ () of the __ __init () in the triangle).
>>> isinstance(t,Triangle)
True
>>> isinstance(t,Polygon)
True
>>> isinstance(t,int)
False
>>> isinstance(t,object)
True
A good opportunity may be to use super-built (). Therefore, super () .__ init __ (3) similar to the quotes .__ init __ (axis, 3) is preferred. You can learn more about super () work in Python.
Two built-in staff assistance () and issubclass () are used to check the inheritance. Instance () returns true if the object is for example in the class or other classes. Each and every Python class is between the elementary classes.
The subclass () is used to check the inherited class.
>>> issubclass(Polygon,Triangle)
False
>>> issubclass(Triangle,Polygon)
True
>>> issubclass(bool,int)
True
Comments
Post a Comment