Home  >  Article  >  Backend Development  >  what is python method

what is python method

藏色散人
藏色散人Original
2019-10-25 11:16:133009browse

what is python method

What is the python method?

Methods are used to describe the behavior of objects.

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 objects.

The names of private methods start with two underscores __.

Each object has its own public and private methods, and members belonging to classes and objects can be accessed in these two types of methods.

Public methods are called directly through the object name.

Private methods cannot be called directly through the object name. They can only be called through self in the instance method or 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 you call a public property belonging to the object through the class name externally, method, you need to explicitly pass an object name to the self parameter of the method to clearly specify which object's data members are 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.

Recommendation: "python tutorial"

>>>class Root:
__total = 0
def __init__(self, v): #构造函数
self.__value = v
Root.__total + = 1
def show(self): #普通实例方法
print(‘self.__value:’,self.__value)
print(‘Root.__total:’,Root.__total)
@classmethod #修饰器,声明类方法
def classShowTotal(cls): #类方法
print(cls.__total)
@staticmethod #修饰器,声明静态方法
def staticShowTotal(): #静态方法
print(Root.__total)
>>>r = Root(3)
>>>r.classShowTotal() #通过对象来调用类方法
1
>>>r.staticShowTotal() #通过对象来调用静态方法
1
>>>r.show()
self.__value:3
Root.__total:1
>>>rr = Root(5)
>>>Root.classShowTotal() #通过类名调用类方法
2
>>>Root.staticShowTotal() #通过类名调用静态方法
2
>>>Root.show() #通过类名直接调用实例方法,报错

Error reporting

>>>Root.show(r) #调用方法并访问实例成员
self.__value:3
Root.__total:2
>>>r.show()
self.__value:3
Root.__total:2
>>>Root.show(rr) #通过类名调用实例方法时为 self 参数显式传递对象名
self.__value:5
Root.__total:2
>>>rr.show()
self.__value:5
Root.__total:2

Note: Data members of different object instances do not affect each other and are not shared. of. But all instance methods of the same class are shared between different objects. All objects execute the same code, and the self parameter is used to determine which object's data is to be processed.

In python, there is a difference between functions and methods. A method generally refers to a function bound to a specific instance. When a method is called through an object, the object itself will be passed as the first parameter. Ordinary functions do not have this feature.

>>>class Demo
pass
>>>t = Demo()
>>>def test(self, v):
self.value = v
>>>t.test = test #动态增加普通函数
>>>t.test
>>>t.test(t,3)
>>>print(t.value)
3
>>>import types
>>>t.test = types.MethodType(test, t) #动态增加绑定的方法
>>>t.test
>>>t.test(5)
>>>print(t.value)
5

The above is the detailed content of what is python method. 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:How to install python3Next article:How to install python3