Last Updated:

OOP: Creating and Working with Classes in Python

Python

Introduction

The paradigm of object-oriented programming (OOP) is now widely used. Why is it so good? When using the object-oriented approach in the process of writing code, the developer builds the logic so as to operate with objects. Why objects? The object is most convenient for perception, as it is similar to an element of the real life familiar to us. Each object has its own parameters, properties, features, different and identical features when comparing them with other objects. Each object is unique in some way, but many objects have similarities with other objects. So, there are parameters, attributes, properties that are identical for a certain set of objects. That's how the concept of class came about.

A class is a set of data and methods for working with it. This is the most correct and generally accepted definition. Simply put, a class is "something with something" or a "cocktail." The cocktail consists of ingredients. Certain volumes of cocktails can be used to create other cocktails. And so on... options for interpreting the concept of "class" - darkness. As a result, the class is a data type. New class — new type.

An object is a unit of something that can be thought of as an independent and sufficient component. It sounds complicated, but in fact it is easier to show. Let's take a hackneyed example – a car.

 

You can drive a car. When we say "car" to someone, the interlocutor will immediately understand what is going on (unless, of course, he is a guest from the distant past) and there is no need to explain that the car is a set of nuts, rivets, gears assembled strictly according to a specific scheme in various spare parts, which were then attached to other spare parts that are not at all similar to each other, and the resulting designs were beautifully and elegantly covered with various polymers. A car is a car. It can be a tractor, and a racing car, and a Chevrolet Camaro of green color (in the image - he).

A method is a kind of unambiguous way of processing data.


Analogy

 

For the most complete understanding, we use an analogy. A class is a car. The object is a tractor or a racing car or a Chevrolet Camaro. Every tractor is a car, but not every car is a tractor. Agree, the car in the image above does not look like a tractor at all, but at the same time they are both cars.

What do these two cars have in common? They can both go back and forth, turn right and left, have a driver's seat, bibikat. There are similarities – there are differences. The tractor has one seat and the Camaro has two. The tractor has large wheels, and the Camaro has medium-sized. The tractor's front and rear wheels are different in size, the Camaro has all four wheels of the same size. One class can serve as the basis for another class.


Create your own class

To create your class in Python, you use a construct that starts with the service word class. First, the service word is prescribed, then the name of the class. In Python, classes are called capitalized words separated by an underscore character.

Create a class does absolutely nothing

This is how the class is announced.

Let's add to our class a few fields and methods that will interact with this data and one method that will be like Uncle Fedor from the great cartoon – by itself.

A class field is a variable that is created when the class is initialized.

A class method is a function in the context of OOP.

The field values are set to None, which means that there is nothing there. According to the Philosophy of Python, all variables should be declared, so in order not to assign a default numeric value, we will assign a "void".


Setters and getters

The names of the methods that write the value to the field and take the value of the field begin with set and get. These are the so-called setters and getters. These are generally accepted terms that are used to unambiguously describe specific processes. If we write, we use a setter, if we take it, we use a getter. One setter and one getter are used per field. But it is not necessary to use both of them, if the field is a constant, then a setter is simply not required.

It is possible to refer to the field by its name through a period, but this approach is unsafe. Change can occur at any given time and control is only possible through the implementation of hatters and setters for each class field, where necessary. Do not write them if they are not required.

When is it not necessary to create setters and/or getters?

  • if an immutable value (constant) is stored in the field
  • if the field is used by the internal mechanisms of the class

Calling Class Methods

To call class methods, you must:

  • create a My_class object
  • through the point to access the methods of the class

In the console we get

 

  • A value of 3 is the value of the right_value field
  • The value of None is the value of the left_value field, the value of None was assigned initially and it has not changed due to the fact that...
    • ... that we passed the symbol three, not the number
    • ... in the setter there is a validation (check) for belonging to a numerical type
  • A value of 45 is the result of addition 43 and 2

So you can just create classes and their fields with methods.