Home Backend Development Python Tutorial what is python method

what is python method

Oct 25, 2019 am 11:16 AM
python method

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() #通过类名直接调用实例方法,报错
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles