python中的@classmethod用法

Python classmethod()

该方法返回给定函数的类方法classmethod()

什么是类方法?

类方法是绑定到类而不是其对象的方法。它不需要创建类实例,就像静态方法一样。

静态方法和类方法的区别在于:

  • 静态方法对类一无所知,只处理参数;
  • 类方法适用于类,因为它的参数始终是类本身;
  • 类方法既可以由类调用,也可以由其对象调用。

下面是一个例子:

python 复制代码
class damn():
    total_damage = 0
    def compute_damage(self, gears: int, base_damage: int):
        self.total_damage = base_damage**gears
        print(f"Gears{gears}! 造成了{self.total_damage}点伤害!")

damn.damage = classmethod(damn.compute_damage)
damn.damage(3, 64)
# Outputs: Gears3! 造成了262144点伤害!

@classmethod

python 复制代码
# 用法
@classmethod
def func(cls, args...)
	# cls接受类作为参数,而不是类的对象或者实例

例子:

python 复制代码
from datetime import date

# random Person
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def fromBirthYear(cls, name, birthYear):
        return cls(name, date.today().year - birthYear) 
        # 返回带有初始化参数的Person实例

    def display(self):
        print(self.name + "'s age is: " + str(self.age))

person = Person('luffy', 25)
person.display()

person1 = Person.fromBirthYear('Kaido',  1900)
person1.display()

# OUTPUTS:
	luffy's age is: 25
	Kaido's age is: 124

可以看出,它类似与C++中的函数重载。

在深度学习模型代码中用于超参数载入

继续举例子:

python 复制代码
class luffy():
    def __init__(
        self,
        enemy_name: str,
        gears: int,
        base_damage: int,
        enemy_health: int,
    ) -> None:
        self.enemy_name = enemy_name
        self.gears = gears
        self.base_damage = base_damage
        self.enemy_health = enemy_health
    
    def print_info(self):
        print(f"enemy_name: {self.enemy_name}, gears: {self.gears}, base_damage: {self.base_damage}, enemy_health: {self.enemy_health}")
    
    def battle_info(self):
        num = self.base_damage**self.gears
        if num >= self.enemy_health:
            print(f"路飞使用了{self.gears}档攻击,造成了{num}点伤害,{self.enemy_name}被击溃了!")
        else:
            print(f"路飞使用了{self.gears}档攻击,造成了{num}点伤害,{self.enemy_name}还剩下{self.enemy_health - num}点生命值!")
    
    @classmethod
    def init_from_class_method(cls, luffyname, conf):
        args = {}
        args['enemy_name']      = conf[luffyname]['enemy_name']
        args['gears']           = conf[luffyname]['gears']
        args['base_damage']     = conf[luffyname]['base_damage']
        args['enemy_health']    = conf[luffyname]['enemy_health']
        return cls(**args)

以上代码定义了一个类,用来描述一场对决信息,实例中的数值则存放在config.yaml配置文件中:

yaml 复制代码
luffy1:
  enemy_name: "BigMom"
  gears: 3
  base_damage: 12
  enemy_health: 1500
luffy2:
  enemy_name: "Kaido"
  gears: 4
  base_damage: 12
  enemy_health: 25000
luffy3:
  enemy_name: "Kaido"
  gears: 5
  base_damage: 12
  enemy_health: 25000

我们把超参加载的部分封装在类方法中,接下来是如何实例加载演示:

python 复制代码
config_path='practice/config.yaml'
conf = yaml.safe_load(open(config_path))
luffy1 = luffy.init_from_class_method('luffy1', conf)
luffy1.battle_info()
luffy2 = luffy.init_from_class_method('luffy2', conf)
luffy2.battle_info()
luffy3 = luffy.init_from_class_method('luffy3', conf)
luffy3.battle_info()

终端输出结果如下:

powershell 复制代码
路飞使用了3档攻击,造成了1728点伤害,BigMom被击溃了!
路飞使用了4档攻击,造成了20736点伤害,Kaido还剩下4264点生命值!
路飞使用了5档攻击,造成了248832点伤害,Kaido被击溃了!

代码范例

python 复制代码
class M2T2(nn.Module):
    def __init__(
        self,
        backbone: nn.Module,
        transformer: nn.Module,
        object_encoder: nn.Module = None,
        grasp_mlp: nn.Module = None,
        set_criterion: nn.Module = None,
        grasp_criterion: nn.Module = None,
        place_criterion: nn.Module = None
    ):
        super(M2T2, self).__init__()
        self.backbone = backbone
        self.object_encoder = object_encoder
        self.transformer = transformer
        self.grasp_mlp = grasp_mlp
        self.set_criterion = set_criterion
        self.grasp_criterion = grasp_criterion
        self.place_criterion = place_criterion

    @classmethod
    def from_config(cls, cfg):
        args = {}
        args['backbone'] = PointNet2MSG.from_config(cfg.scene_encoder)
        channels = args['backbone'].out_channels
        obj_channels = None
        if cfg.contact_decoder.num_place_queries > 0:
            args['object_encoder'] = PointNet2MSGCls.from_config(
                cfg.object_encoder
            )
            obj_channels = args['object_encoder'].out_channels
            args['place_criterion'] = PlaceCriterion.from_config(
                cfg.place_loss
            )
        args['transformer'] = ContactDecoder.from_config(
            cfg.contact_decoder, channels, obj_channels
        )
        if cfg.contact_decoder.num_grasp_queries > 0:
            args['grasp_mlp'] = ActionDecoder.from_config(
                cfg.action_decoder, args['transformer']
            )
            matcher = HungarianMatcher.from_config(cfg.matcher)
            args['set_criterion'] = SetCriterion.from_config(
                cfg.grasp_loss, matcher
            )
            args['grasp_criterion'] = GraspCriterion.from_config(
                cfg.grasp_loss
            )
        return cls(**args)

	...

模型创建与加载部分代码:

python 复制代码
model = M2T2.from_config(cfg.m2t2)
ckpt = torch.load(cfg.eval.checkpoint)
model.load_state_dict(ckpt['model'])
model = model.cuda().eval()
相关推荐
Hard but lovely2 分钟前
C++:stl-> list的模拟实现
开发语言·c++·stl·list
白露与泡影5 分钟前
Spring容器初始化源码解析
java·python·spring
码界筑梦坊31 分钟前
98-基于Python的网上厨房美食推荐系统
开发语言·python·美食
光爷不秃41 分钟前
Go语言中安全停止Goroutine的三种方法及设计哲学
开发语言·安全·golang
计算机源码社1 小时前
分享一个基于Hadoop的二手房销售签约数据分析与可视化系统,基于Python可视化的二手房销售数据分析平台
大数据·hadoop·python·数据分析·毕业设计项目·毕业设计源码·计算机毕设选题
lpfasd1231 小时前
非中文语音视频自动生成中文字幕的完整实现方案
开发语言·python
大志说编程1 小时前
LangChain框架入门15:深度解析Retrievers检索器组件
python·langchain·llm
hqwest2 小时前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
NEUMaple2 小时前
python爬虫(四)----requests
开发语言·爬虫·python
sufu10652 小时前
说说内存泄漏的常见场景和排查方案?
java·开发语言·面试