Home  >  Article  >  Backend Development  >  What is the method in python

What is the method in python

silencement
silencementOriginal
2019-05-20 18:15:4418367browse

What is the method in python

Methods are used to describe the behavior of an object.

Methods defined in a class can be roughly divided into four categories: public methods, private methods, static methods, and class methods.

  • Public methods and private methods generally refer to instance methods belonging to an object.

  • The name of a private method starts with two underscores __.

  • Each object has its own public and private methods. In these two types of methods, you can access members belonging to classes and objects.

  • Public methods are called directly through the object name,

  • Private methods cannot be called directly through the object name, and can only be called through self in the instance method Or call it externally through special methods supported by python.

  • All instance methods of a class must have at least one parameter named self, and must be the first formal parameter of the method. The self parameter represents the object itself.

You need to prefix self when accessing instance properties in an instance method of a class, but you do not need to pass this parameter when calling an object method externally through the object name. If When externally calling a public method belonging to an object through a class name, you need to explicitly pass an object name to the self parameter of the method to clearly specify which object's data member is to be accessed.

Both static methods and class methods can be called through class names and object names, but they cannot directly access members belonging to the object, only members belonging to the class. Generally, cls is used as the first parameter of a class method, representing the class itself. There is no need to pass a value for this parameter when calling the class method.

Related learning recommendations: python tutorial

The above is the detailed content of What is the method in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is int in pythonNext article:What is int in python