📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #17 – Основы ООП. Создание класса и объекта

Школа itProger / Программирование15:00

Transcription

Hello everyone! During the lesson, we will discuss the topic of object-oriented programming. We will cover the theory and also explore the practical use of APIs.

At the initial stage, OOP can seem like a dark forest where much is unclear and overly complicated. However, it’s not as scary as it seems. I suggest we abstract from specific, confusing definitions and get acquainted with the concepts in simple terms.

For example, let's take a robot that we will try to describe using classes. In this case, a class is just a blueprint of the robot where we describe its main characteristics. An object, on the other hand, is a complete robot created according to that blueprint, or in other words, based on the class.

Inheritance is the addition of useful options to the robot's blueprint. For instance, we take a standard robot blueprint and add lasers, wings, or armor to it. All these additions will be made in a subclass, which inherits the main functionality from the parent class.

Polymorphism refers to the common functionality for all robots, regardless of how different they may be from one another. For example, in the main class, we specify the ability to move for all subsequent robots. In the subclass, we can additionally specify the ability to levitate for one robot, while in another class, we can specify the ability to move on water, and so on.

Thus, there is a common functionality recorded in the main blueprint, but it can be rewritten for each subsequent robot, for each subsequent subclass.

Encapsulation acts as armor protecting the robot. Under the armor, there are vulnerable elements like wires and microchips. After being covered by armor, which involves different access modifiers like protected, the robot will be fully protected from external interference. Essentially, we provide access to all fields or variables only through methods, thereby closing direct access to the field.

Another nuance is that a field and a variable are essentially the same thing. If we talk about a variable outside a class, it’s just a regular variable. But if we talk about a variable inside a class, it’s referred to as a field. However, in fact, they are the same.

In all classes, methods can differ, as can fields and constructors. Each class allows the creation of any number of different objects, each with its own characteristics.

Using OOP is very convenient because we can describe all the functionality for a specific action or object in one place. For example, if we are making a game and need to describe the behavior of a car, we could create dozens of methods, variables, and arrays and scatter them across different files. While that might sound okay, it’s not particularly efficient.

Instead, we create a separate class, describe all methods, variables, and other nuances in one place, and based on that class, we can create as many objects as we want. Each object is a specific car with common characteristics defined in the class, but it can also have its own unique values.

Thus, classes and objects allow us to clearly delineate different sections of code that describe specific actions in the project and specific objects.

Now, let’s move from words to action. To create a class, we need to write the keyword "class," followed by the class name. The name can be anything, as long as we don’t use special characters.

For example, I want to create a class that describes cats. Each object created based on this class will represent a specific cat with certain characteristics. Inside the class, we will describe standard global characteristics for all our future cats. Since the class will describe cats, let’s name it accordingly.

Next, we write a colon, and inside the class, we can create some fields. If we are talking specifically about a class, these are called fields, although they are essentially the same as variables.

Let’s create a few fields. For instance, we can create a field called "name," and initially, I won’t add any values, so I will just set it to an empty value.

We might also want to store the age of each cat and whether our specific cat is happy or not. Each time I create an object based on this class, I will be able to add a name, age, and happiness status.

You can also store other values here, such as lists, dictionaries, tuples, and so on. All these data types we have studied can be used here.

Now, let’s create a few objects based on this class. For example, we create our first object, which can be named anything. I will name it "cat1."

Next, we need to specify its values. To create this object, I simply refer to the class by its name and add parentheses at the end. For now, I won’t add anything inside the parentheses, but in the next lesson, we will study constructors, and then we will pass various parameters there. For now, they will just be empty.

Now, based on this object, we can access various properties defined in the class. Here, I will refer to "cat1" and let’s initially give this cat a name. Let’s call our first cat "Barsik," a classic name.

Next, let’s set the age of our first cat to three years, and we will also set the happiness status to true, meaning this cat is happy.

Now, let’s create another cat. This cat will be named "cat2." Similarly, I will allocate memory by referring to the class name and adding parentheses.

For this second cat, I will specify different values. For instance, let’s name it "Jasper," and set its age to two years. As for the happiness status, we will set it to false because, with a name like that, how could it be happy?

So now we have two different objects. Both were created based on the same class, but they have completely different characteristics: different names, different ages, and different happiness statuses.

This means that all the descriptions for our objects are located in one single place. If I need to add any functionality, I will write it in this one place, and based on this single place, I can create different objects—in our case, different cats.

All these cats, all these objects, possess all the necessary characteristics because everything is described in one place. To verify this, let’s refer to "cat1" and also to "cat2" to check their names.

If we run this now, we will see that they have different names, confirming that they are indeed two different objects. Despite being created from the same class and referring to the same field, they have different values because they are simply different objects.

In summary, classes and objects are convenient because, through a class, we can describe common characteristics in one single place without specifying concrete values. Then, we can create various objects, each with the necessary characteristics and values. Plus, all the common functionality and descriptions will not be scattered throughout the program in different places; they will always be in one single place—in one specific class.

This is extremely convenient. I understand that the class fully describes everything related to cats, and then we just create different objects that possess characteristics but do not have any additional functionality that should be described here. All additional functionality is described specifically in this class.

Now, let’s add some additional functions to this class, which will be immediately available for both the first and second objects.

Here, let’s define a function called "set_data." This function will accept several parameters and set these parameters to the fields. You may notice that as soon as I wrote "set_data," another parameter called "self" was automatically added. This parameter must always be included because it indicates that this method is inside the class and can access various fields within the class.

So, I will keep this parameter and not remove it. With this parameter, I can later refer to these values inside the class.

Now, I will specify which additional parameters I want to accept. For example, I want to accept three parameters: the name of the cat, its age, and whether it is happy. The names of these parameters can be anything, but it’s common practice for parameter names to match the field names.

So, I will name them "name," "age," and "is_happy." Next, I want to get these values and set them as the values for the fields.

However, if I write something like "name = name," the interpreter will throw an error because it won’t understand what value we are assigning where. To clarify that we are setting the value of the field belonging to this class, we need to write "self.name = name." This way, we indicate that we are referring to the field "name" of this class and setting it to the value passed to the function.

We will do the same for the age field and the happiness status field.

Now, instead of breaking this down into three lines of code, I can simply refer to "cat1" and call the "set_data" method, passing three parameters: the name, age, and happiness status.

I can now delete the previous three lines of code because all these values are now being assigned in one line.

Let’s also set the name in Russian for better clarity. We can do the same for our second cat. Here, we will call "set_data" and specify the three parameters.

Now, I can safely delete all the previous lines of code. If I run this now, everything will process correctly, and the values will be set through "set_data."

Next, let’s add another method called "get_data." The name can be anything, but I will use this name. This method won’t accept any parameters but will return the full characteristics of a specific object.

Here, we will refer to the name, and we might also want to output something like a space, and then we will output the age of the cat. We might also want to indicate whether the cat is happy.

Now, I can call this method for both the first and second objects, and it will provide the full information for each object.

If I call "get_data" for the first object, it will output the values for that object, and if I call it for the second object, it will output the values for that one.

Thus, we have implemented a common class that contains various characteristics and functions, allowing us to create as many different objects as we want. Each of these objects will have access to all these functions and characteristics, and the values for each object can differ.

In fact, each object will be a separate cat with all the capabilities described in one single place. Classes are a tool that maximally reduces code and increases code readability.

Well, that concludes our lesson. I hope you enjoyed it! If so, don’t forget to subscribe to the channel and join our social media. All the links will be in the description of this video.

That’s all from me. See you soon! Goodbye!