当前位置: 首页 > news >正文

Python中类静态方法:@classmethod/@staticmethod详解和实战示例

在 Python 中,类方法 (@classmethod) 和静态方法 (@staticmethod) 是类作用域下的两种特殊方法。它们使用装饰器定义,并且与实例方法 (def func(self)) 的行为有所不同。


1. 三种方法的对比概览

方法类型是否访问实例 (self)是否访问类 (cls)典型用途
实例方法✅ 是❌ 否访问对象属性
类方法 @classmethod❌ 否✅ 是创建类的替代构造器,访问类变量等
静态方法 @staticmethod❌ 否❌ 否工具函数,与类逻辑相关但不依赖类或实例

2. 实例方法(默认方法)

class MyClass:def instance_method(self):print(f"This is an instance method. self = {self}")obj = MyClass()
obj.instance_method()  #  输出 self 为该实例

3. 类方法 @classmethod

class MyClass:class_var = "Hello, Class!"@classmethoddef class_method(cls):print(f"This is a class method. cls = {cls}")print(f"Access class_var = {cls.class_var}")MyClass.class_method()  #  通过类调用
obj = MyClass()
obj.class_method()      #  通过实例也可以调用

🔹 特点:

  • 第一个参数是 cls,代表类本身

  • 常用于:

    • 构造不同初始化方式
    • 修改/访问类变量
    • 工厂模式 (from_* 等)

示例:类方法作为工厂方法

class Book:def __init__(self, title, author):self.title = titleself.author = author@classmethoddef from_string(cls, book_str):title, author = book_str.split(",")return cls(title.strip(), author.strip())book = Book.from_string("1984, George Orwell")
print(book.title)  # ➜ 1984

4. 静态方法 @staticmethod

class MyMath:@staticmethoddef add(x, y):return x + yprint(MyMath.add(3, 5))  # ➜ 8

特点:

  • 不接收 selfcls 参数

  • 和普通函数类似,但归属在类中,逻辑上与类相关

  • 常用于:

    • 与类操作相关的工具方法
    • 不需要访问类或实例内部状态

示例:静态方法作为工具函数

class Temperature:def __init__(self, celsius):self.celsius = celsius@staticmethoddef to_fahrenheit(c):return c * 9 / 5 + 32def show(self):print(f"{self.celsius}°C = {self.to_fahrenheit(self.celsius)}°F")temp = Temperature(25)
temp.show()  # ➜ 25°C = 77.0°F

总结:何时用哪种方法?

场景推荐方法
需要访问或修改实例属性实例方法
需要访问或修改类变量、类构造类方法 @classmethod
工具函数:与类相关但不访问类或实例成员静态方法 @staticmethod

@classmethod@staticmethod 应用场景实战

利用 Python 中的 @classmethod@staticmethod 可以优雅地实现设计模式,如单例模式工厂模式。下面我将分别讲解这两种模式的原理、适用场景,并结合代码示例说明如何使用类方法或静态方法实现。


1、工厂模式(Factory Pattern)

定义:工厂模式是一种创建对象的设计模式,它允许类在创建对象时不暴露实例化逻辑,而是通过类方法返回不同的类实例。

使用类方法实现工厂模式

class Animal:def __init__(self, name):self.name = name@classmethoddef create_dog(cls):return cls("Dog")@classmethoddef create_cat(cls):return cls("Cat")# 使用工厂方法创建对象
dog = Animal.create_dog()
cat = Animal.create_cat()print(dog.name)  # ➜ Dog
print(cat.name)  # ➜ Cat

应用场景

  • 多种初始化方式(如从字符串、文件、数据库等)
  • 根据条件返回不同子类对象

更复杂示例:带注册机制的工厂

class ShapeFactory:_creators = {}@classmethoddef register(cls, name, creator):cls._creators[name] = creator@classmethoddef create(cls, name, *args, **kwargs):if name not in cls._creators:raise ValueError(f"Unknown shape: {name}")return cls._creators[name](*args, **kwargs)# 各种 Shape 类
class Circle:def __init__(self, radius):self.radius = radiusclass Square:def __init__(self, length):self.length = length# 注册
ShapeFactory.register("circle", Circle)
ShapeFactory.register("square", Square)# 创建对象
c = ShapeFactory.create("circle", 5)
s = ShapeFactory.create("square", 3)
print(c.radius)  # ➜ 5
print(s.length)  # ➜ 3

2、单例模式(Singleton Pattern)

定义:单例模式保证类在程序中只有一个实例。

使用类方法实现单例

class Singleton:_instance = None@classmethoddef get_instance(cls):if cls._instance is None:cls._instance = cls()return cls._instance# 测试
a = Singleton.get_instance()
b = Singleton.get_instance()
print(a is b)  # ➜ True

也可以结合 __new__ 实现单例

class Singleton:_instance = Nonedef __new__(cls, *args, **kwargs):if not cls._instance:cls._instance = super().__new__(cls)return cls._instance# 所有实例是同一个
a = Singleton()
b = Singleton()
print(a is b)  # ➜ True

3、静态方法在设计模式中的作用

工具类模式(Utility Pattern)

静态方法常用于实现工具类(即所有方法都与对象状态无关,仅执行逻辑计算)。

class MathUtils:@staticmethoddef add(x, y):return x + y@staticmethoddef multiply(x, y):return x * yprint(MathUtils.add(3, 4))       # ➜ 7
print(MathUtils.multiply(3, 4))  # ➜ 12

4、小结

设计模式推荐使用原因
工厂模式@classmethod需要访问类构造器、支持多种创建方式
单例模式@classmethod / __new__控制实例生成的唯一性
工具类@staticmethod无需访问类/实例,仅提供辅助功能

http://www.lryc.cn/news/585365.html

相关文章:

  • C#中的设计模式:构建更加优雅的代码
  • 链接代理后无法访问网络
  • C++入门基础篇(二)
  • HandyJSON使用详情
  • 使用Spring Boot和PageHelper实现数据分页
  • Excel快捷键
  • 20250710-2-Kubernetes 集群部署、配置和验证-网络组件存在的意义?_笔记
  • leetcode:377. 组合总和 Ⅳ[完全背包]
  • 代账行业数字化破局:从“知道”到“做到”,三步走稳赢!
  • RK3566/RK3568 Android11 修改selinux模式
  • 森马服饰从 Elasticsearch 到阿里云 SelectDB 的架构演进之路
  • 【牛客刷题】超级圣诞树(递归法和分形复制法)
  • TCP服务器与客户端三种方法实现
  • Java使用OSHI获取服务器信息
  • 网络安全基础作业
  • python学习DataFrame数据结构
  • 无锁队列:从零构建生产者-消费者数据结构
  • 第十七节:第三部分:网络通信:UDP通信,一发一收,多发多收
  • 汽车级MCU选型新方向:eVTOL垂桨控制监控芯片的替代选型技术分析
  • aaa认证
  • lora网关
  • 如何选择数据可视化工具?从设计效率到图表表现力全解读
  • OD(OllyDbg)使用介绍
  • day02-数组part02
  • 网络安全初级小练
  • 【前端】【组件库开发】【原理】【无框架开发】现代网页弹窗开发指南:从基础到优化
  • Oracle字符类型详解:VARCHAR、VARCHAR2与CHAR的区别
  • 业务建模如何让金融数字化转型 “轻” 装上
  • 林吉特危机下的技术革命:马来西亚金融系统升维作战手册
  • axios 与 fetch 的区别