The self parameter in Python and its role

In Python, the self parameter is a conventional parameter name used to represent the object itself. It is usually used as the first parameter of a method and is used to refer to the object on which the method is currently being called.

In object-oriented programming, the function of the self parameter is to allow the object to access its own properties and methods. By using the self parameter, we can reference the properties of the object inside the methods of the class and call other methods of the object. In fact, self can be seen as a reference to an instance of a class.

To better understand the role of the self parameter, let's look at a simple example. Suppose we have a class called Person that has an attribute name and a method say_hello:

class Person:
    def __init__(self, name):
        self.name = name

    def say_hello(self

Guess you like

Origin blog.csdn.net/2301_78484069/article/details/133322129