What is the essence of Self in the OOP of the Python and this in C ++ (better in examples)?
question@mail.ru
·
01.01.1970 03:00
The meaning of this in C ++ / Self in Python
answer@mail.ru
·
01.01.1970 03:00
It is well noted that these concepts have the same role in OOP and object-oriented programming.: this is a reference of the object to itself. It allows you to:
- Access your own fields (i.e., variables) and methods;
- Transfer the object itself to other functions and objects.
Python and C++ differ in that the first part in C++ (also in Java, C# and Objective-C) is implemented automatically, we don't have to constantly prescribe self or this. However, this creates either confusion between object variables and local variables, or the constant use of underscores in variable names, which, in my opinion, is not much better than referring to an object. On the contrary, in Python (and, for example, JavaScript), it is always necessary to explicitly specify if access is to the field of the current object.
Let's consider both use cases.
1) To access your fields
Let's take the class rectangle, it has two variables with side lengths: a and b, we need a method to calculate the area; and to do this, you need to access the fields of the object from the method. Python code:
class/span> Rect: def class="">def __init__(self, a, b): # the presence or absence of self distinguishes # local variables and fields of the object self.a = a self. self. = a selfb = b # self is clearly present in the arguments of the methods des="">def area(self): <>): retu self.a * self.b There are different ways to do this in C++. Option A:
class/span> Rect { private: int _a, _b; public: Rectspan>: Rect(int a, int b) { // the underscore is used to, to //distinguish arguments/local variables // and object fields _a = a; _b = b; } int">int ass="">area() { // this is not in the arguments of the method, // nor in accessing the fields of the object retu _a * _b; }}C++, option B:
class/span> Rect { private: int a, b; public: Rectspan>: Rect(int a, int b) : a(a), b (b) { // this way of initializing object fields // avoids confusion with arguments } int">int ass="">area() { // also, to access your own variables // you can use this retuass="">retu this->a * this-gt;b; }}>Lyrical digression
Note that in Python methods self is always specified explicitly, while in C++ this takes the value automatically. And although the construction uses the same object.method(data), and in the declaration of methods this is not used, in fact, the reference to the object is present as a hidden first argument. You can also specify the object explicitly, here are two equivalent lines of C++ code:
my_object.push(3);MyClass::push class="">push(&my_object, 3);// I hope you understand the meaning of the characters in C++; and *// if not, hurry up and find outde>
For
my_objectas an instance of this type of class:class MyClass { ...void push(int number) { ... }}By the way, similarly, you can explicitly specify an object in Python.:
#implicit object transfer to the first argumentmy_object.push(2)# explicitly specifying the object as an argumentMyClass.push(my_object, 2)2) To pass the current object
object
For example, we have some kind of controller object or container, and objects of our class should automatically rely on it or be deleted from it. Python:
all/span> = []class>class MyClass: def __init__(self): all.append(self)C++:
class MyClass {public: MyClass();}vector<MyClass*> all = new/span> vector>vector<MyClass*>();MyClass::MyClass() { all.push(thi