python中的classmethod进行注释

324次阅读
没有评论
python中的classmethod进行注释

python中的classmethod进行注释

在进行注解的时候,如果是针对于类方法这方面,我们可以选择classmethod函数。在类的调用上比较灵活,不局限于类或者是实例。特别是有些小伙伴害怕的参数问题,使用classmethod函数后也不用太多的考虑这方面的因素。下面我们就classmethod函数用法等各个方面展开讲解。

1.用法:

class C:
    @classmethod
def f(cls, arg1, arg2, ...): ...

2.参数

3.返回值

返回函数的类方法。

4.classmethod 实例

class Data_test2(object):
    day=0
    month=0
    year=0
    def __init__(self,year=0,month=0,day=0):
        self.day=day
        self.month=month
        self.year=year
 
    @classmethod
    def get_date(cls, string_date):
        #这里第一个参数是cls, 表示调用当前的类名
        year,month,day=map(int,string_date.split('-'))
        date1=cls(year,month,day)
        #返回的是一个初始化后的类
        return date1
 
    def out_date(self):
        print "year :"
        print self.year
        print "month :"
        print self.month
        print "day :"
        print self.day

通过本篇学习,我们除了能使用classmethod进行注释外,在参数的问题上也能得到很好地解决,小伙伴们学会后可以逐个去课外拓展。‍

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-14发表,共计860字。
新手QQ群:570568346,欢迎进群讨论 Python51学习