In this article, we intend to introduce you to the concept and nature of object orientation, its application in Python science, and its programming principles. If you are reading this article, you are probably not familiar enough with the concept of object orientation. So, first, we will review the concepts and nature of this programming model, and then we will discuss its application in Python. Because Python has been one of the object-oriented languages since its inception.
Object-oriented is a model of programming in which objects and data are used instead of defining functions and logic. In this model of programming, each object can be considered a data model that has unique characteristics. For example, if we consider humans as an object in object-oriented programming, they have unique characteristics such as name, surname, phone number, address, etc., each of which is considered an information field in programming.
Object-oriented programming is more popular among programmers than another programming due to its flexibility and high efficiency.
In object-oriented programming, the first step for the programmer is to identify the objects that he wants to manipulate during the program. Once an object is recognized, a class of objects is created for it, which contains data types that can manipulate this logical sequence.
Therefore, object-oriented programming focuses on objects that developers can manipulate, not on their logic. This approach is very useful in programming large and complex programs because we can divide a large project into smaller parts and work as a group on a complex and large project.
Table of Contents
Principles of object-oriented programming:
Object-oriented programming is based on the following four cases:
The properties of each object are placed in the scope and class of the object itself. Objects no longer have the power to access and modify class data and can only access a list of publicly defined class functions. This feature in object-oriented programming has increased security and prevented unwanted corruption of information.
With the property of abstraction, in object-oriented programming, no code is written in the main body, and for this reason, we cannot directly create objects from abstract classes. The abstract concept, because it does not have specific code, helps programmers to easily make changes and additions over time. For example, if we have a print method in a program, we cannot define the body of this method. Then create child classes from this method and define body codes in these child classes and easily create objects from them.
In the concept of inheritance, we can implement unique objects and relationships for an object. If these features are duplicated in another object, it can easily inherit common features from its higher class without damaging the unique properties of both. Come in This feature in object-oriented programming provides a more accurate data analysis to the programmer and reduces the development time and increases the level of accuracy. For example: if we consider a human being as an object, the father of the family may have colored hair and colored eyes.
Using the concept of polymorphism, we can easily define a public method that has different inputs each time. For example, if we consider humans as objects, they all eat, then we can consider a method called FOOD for them, but we must be careful that not all people eat the same foods. Therefore, the FOOD method can be used every time and for each person with different inputs.
These four principles are object-oriented programming principles that are used in this programming model. Therefore, it can be said that with object-oriented programming, coding becomes very flexible and organized. For this reason, programmers prefer this model to the old models. In the following article, we will only name a few programming languages that are based on the principles and concepts of object orientation and use the concept of object orientation.
Object-oriented programming languages
Some important terms
Class: It is a prototype that is written by the programmer and includes attributes, and methods that each do a specific job.
Class variable: It is a variable that is common among all members of the class. Class variables are defined inside the class and outside the class methods.
Function overloading: assigning more than one behavior to a function. It means that there are several definitions of a function and one of the behaviors is selected based on the parameters that are sent to it.
Inheritance: transferring the properties of one class to other classes.
Example: They say the special object of each class. For example, an object named obj belonging to the circle class is an instance of the circle class.
Instantiation: It is called creating an object from a class.
Method: There are special types of functions that are defined in the class.
Object: A unique instance of a data structure defined by its class.
Operator overloading: assigning more than one behavior to a particular operator.
Object Orientation in Python
Each Python class has internal and predefined attributes that can be accessed like other attributes and methods with the dot operator after the object name. Using these methods can be useful in some cases. These adjectives are:
dict: Returns a dictionary containing the namespaces in the class.
Doc: If you have defined documentation in the class, it will return its text, if you have not defined it, it will return nothing.
Name: Returns the name of the class.
Module: Returns the name of the module in which the class is defined, which returns the value of main in normal and interactive mode.
Bases: Returns a tuple containing the classes it inherits from. In other words, it returns the names of the base classes.
Python automatically deletes objects that are no longer needed to free up the amount of memory occupied by it. The process during which it continuously corrects the memory blocks that are no longer in use is called Garbage Collection. This process is running during the execution of the program and when there is no reference to the object, its trigger is activated.
When we give an object a new name or a new container (tuple, list, dictionary), the number of references to that object increases. When we delete the object with the Del method or when its reference is out of range, the number of references of the corresponding object will be reduced. When the number of references reaches zero, Python automatically deletes that object from memory.