Article Directory
class method, static method
class method
A class method is a method that takes a class object as the first parameter. A decorator is required @classmethod
to identify it as a class method. For class methods, the first parameter must be a class object, generally as cls
the first parameter.
class Dog(object):
__type = "狗"
@classmethod
def get_type(cls):
return cls.__type
scenes to be used:
- When the method needs to use the class object (such as accessing private class attributes, etc.), define the class method.
- Class methods are generally used in conjunction with class attributes.
static method
@staticmethod
Static methods are decorated by decorators , and static methods do not need to pass class objects or instance objects (no formal parameters self/cls
). Static methods can also be accessed through instance objects and class objects.
class Dog(object):
type = "狗"
def __init__(self):
name = None
@staticmethod
def introduce():
print("犬科哺乳动物,属于食肉目..")
scenes to be used
- Static methods are defined when the method neither needs to use instance objects (such as instance objects, instance attributes) nor class objects (such as class attributes, class methods, create instances, etc.).
- Canceling unnecessary parameter passing helps reduce unnecessary memory usage and performance consumption.
important point:
- When a method with the same name is defined in the class, the calling method will execute the last defined method.
class Dog:
def demo_method(self):
print("对象方法")
@classmethod
def demo_method(cls):
print("类方法")
@staticmethod
def demo_method():
print("静态方法")
dog1 = Dog()
Dog.demo_method() # 结果:静态方法
dog1.demo_method() # 结果:静态方法
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model