Object Oriented Programming in Python

Tram Ho

In this article, you will learn about Object Oriented Programming (OOP) in Python and its basics and some examples. You all find out in my article!


Object oriented programming

Python is a multi-paradigm programming language. It supports different programming approaches. One of the common approaches to solving programming problems is creating objects. This is called Object Oriented Programming (OOP).

An object will have two characteristics:

  • Properties
  • Behavior

I have 1 example:

A parrot can be an object, as it has the following properties:

  • Name, age, color will be attributes
  • running, asking or singing would be behavior

In Python, the OOP concept follows some basic principles as follows:


Class

A class is a blueprint for the object. With the Parrot example above, we can imagine the class as a sketch of the Parrot. It will contain all the details about the name, color etc. About the Parrot. class example:

We use the keyword class to define a class Parrot . From the class you have to define detailed properties for describing Parrot .


Object

An object) is an instantiation of a class. When the class is defined it is the description for a defined object.

Here obj is an object of class Parrot .

Now, we will start building Parrot ‘s class and objects.

Create classes and objects in Python

Display value:

In the above program, we created a class with the name Parrot. Then we define the properties. Attributes are a property of an object.

These properties are defined inside the class __init__ method. This is the initialization method that is run first immediately after the object is created.

Next we create instances for the parrot class. In the above example, blu and woo will be the values ​​to refer to the object.

We can access the class property using __class__.species . Similarly we can also access instance properties using blu.name and blu.age


Method

Methods are functions defined within the body of a class. They are used to define the behavior of an object.

Create methods in Python

Display value:

With the above example. We have identified 2 methods as sing() and dance() . they are the methods for the blu object


Inheritance

Inheritance is a way of creating a new class to use the properties of an existing class without modifying it. The newly formed class is a derived class (or subclass). Likewise, an existing class is a base class (or superclass).

Using Inheritance in Python

Display value:

In the above example we create 2 classes: Bird (is the parent class) and Penguin (is the subclass). Here the child class will inherit the functions and properties of the parent class. In the above example, from the subclass we can call swim() from the parent class

Following the above example we was able to modify the behavior of the superclass whoisThis() . Furthermore it is possible to extend the superclass’s function by creating a run()

Additionally, we use the super () function inside the __init __ () . This allows us to run the __init __ () of the parent class inside the subclass.


Encapsulation

Using OOP in Python, it is possible to restrict access to methods and variables. This prevents the data from being directly modified this might be called Package. In Python, we denote private properties by using underscores as prefixes, using _ or __

Packed data in Python

Display value:

In the above example we have defined a computer class. We use the __init__() to set the price for the computer . We tried to fix the price. However, it is not possible to change the price because in Python __maxprice is a private property.

So to change the price we need to use the one setMaxPrice to take the price as an argument.


Polymorphism

Polymorphism in OOP uses a common interface for many data types.

Suppose, we need to color an object, there are many shape options (rectangle, square, circle). However, we can use the same method to color any shape. This concept is called Polymorphism.

Using polymorphism in Python

Display value:

In the example we have defined 2 classes Parrot and Penguin . Each class has a common method called fly() . However the function will be different.

To use polymorphism. We create a fly_test() function that accepts any object and calls the fly() of that object. So when we convert the blu and peggy objects, the function is usable and returns the result.


Conclude

Below I have introduced you to Relative Oriented Programming and specific examples. If you have any questions, please leave a comment below.


See more details

https://www.programiz.com/python-programming/object-oriented-programming

Share the news now

Source : Viblo