Python @classmethod 本质上就是C++ 的静态成员?
Created|Python
一、引言:一个跨语言的困惑当Python 中看到@classmethod 和cls 参数时,C++ 程序员的第一反应是“这有什么用?不就是个普通函数吗?”。但实际上,@classmethod 的存在是为了解决“类级别”的数据共享和操作,这与 C++ 的static 不谋而合。 二、 相同点:都属于“类”而非“对象”无论是Python 的@classmethod 还是 C++ 的static 函数,它们的核心特征都是一致的: 调用方式:都可以不创建实例,直接通过类名来调用 职责范围:它们处理的是与整个类相关的事务,而不是某个具体对象的私有数据 Python 的实现(@classmethod)12345678910111213141516171819202122232425class Person: # 类变量,所有实例共享 count = 0 def __init__(self, name): self.name = name # 每创建一个实例,就调用类方法来增加计数 ...


Recent Posts
Categories
- C++85
- C-Code23
- CMake3
- CS501
- Computer-Networking40
- Computer-Organization1
- Data Structures and Algorithms1
- Data-Structures6
- Essay1
- Essays4
- Foundational Syntax and Core Concepts4
- Git3
- HTTP2
- Interview7
- Lcov2
- Leetcode133
- Linux40
- Linux Internals2
- List1
- Lists1
- MQTT8
- MYSQL3
- MySQL1
- Operating-Systems7
- PKGCONF1
- Python46
- Redis14
- STL3
- UML1
- XML1
- lock1
- practice problems1
- 代码提交1
- 函数模板2
- 工厂模式2
- 文章12
- 热门导读1
- 类图1
- 编译1
- 设计模式8
- 链表1
Archives
- 2026年04月 5
- 2026年03月 7
- 2026年02月 3
- 2026年01月 5
- 2025年12月 11
- 2025年11月 6
- 2025年10月 5
- 2025年09月 15
- 2025年08月 5
- 2025年07月 5
- 2025年06月 5
- 2025年05月 5
- 2025年04月 5
- 2025年03月 5
- 2025年02月 5
- 2025年01月 5
- 2024年12月 6
- 2024年11月 4
- 2024年10月 12
- 2024年09月 11
- 2024年08月 11
- 2024年07月 9
- 2024年06月 9
- 2024年05月 28
- 2024年04月 41
- 2024年03月 41
- 2024年02月 37
- 2024年01月 44
- 2023年12月 5
- 2023年11月 6
- 2023年10月 5
- 2023年09月 4
- 2023年08月 4
- 2023年07月 6
- 2023年06月 5
- 2023年05月 7
- 2023年04月 5
- 2023年03月 5
- 2023年02月 5
- 2023年01月 5
- 2022年12月 6
- 2022年11月 11
- 2022年10月 7
- 2022年09月 4
- 2022年08月 3
- 2022年07月 5
- 2022年06月 7
- 2022年05月 4
- 2022年04月 4
- 2022年03月 4
- 2022年02月 5
- 2022年01月 5
Website Info
Article Count :
477
Runtime :
Total Word Count :
779.8k
Last Update :