python中classmethod和staticmethod的区别

[翻译]PYTHON中STATICMETHOD和CLASSMETHOD的差异

英文原文地址 入口

翻译地址  http://www.wklken.me/posts/2013/12/22/difference-between-staticmethod-and-classmethod-in-python.html


Class vs static methods in Python

这篇文章试图解释:什么事staticmethod/classmethod,并且这两者之间的差异.

staticmethod和classmethod均被作为装饰器,用作定义一个函数为"staticmethod"还是"classmethod"

如果想要了解Python装饰器的基础,可以看 这篇文章

Simple, static and class methods

类中最常用到的方法是 实例方法(instance methods), 即,实例对象作为第一个参数传递给函数

例如,下面是一个基本的实例方法

class Kls(object):
    def __init__(self, data):
        self.data = data

    def printd(self):
        print(self.data)

ik1 = Kls('arun')
ik2 = Kls('seema')

ik1.printd()
ik2.printd()

得到的输出:

arun
seema

调用关系图:

translate1

查看代码和图解:

1/2 参数传递给函数
3   self参数指向实例本身 
4   我们不需要显式提供实例,解释器本身会处理

假如我们想仅实现类之间交互而不是通过实例?我们可以在类之外建立一个简单的函数来实现这个功能,但是将会使代码扩散到类之外,这个可能对未来代码维护带来问题。

例如:

def get_no_of_instances(cls_obj):
    return cls_obj.no_inst

class Kls(object):
    no_inst = 0

    def __init__(self):
        Kls.no_inst = Kls.no_inst + 1

ik1 = Kls()
ik2 = Kls()

print(get_no_of_instances(Kls))

结果:

2

The Python @classmethod

现在我们要做的是在类里创建一个函数,这个函数参数是类对象而不是实例对象.

在上面那个实现中,如果要实现不获取实例,需要修改如下:

def iget_no_of_instance(ins_obj):
    return ins_obj.__class__.no_inst

class Kls(object):
    no_inst = 0

    def __init__(self):
        Kls.no_inst = Kls.no_inst + 1

ik1 = Kls()
ik2 = Kls()
print iget_no_of_instance(ik1)

结果
2

可以使用Python2.2引入的新特性,使用@classmethod在类代码中创建一个函数

class Kls(object):
    no_inst = 0

    def __init__(self):
        Kls.no_inst = Kls.no_inst + 1

    @classmethod
    def get_no_of_instance(cls_obj):
        return cls_obj.no_inst

ik1 = Kls()
ik2 = Kls()

print ik1.get_no_of_instance()
print Kls.get_no_of_instance()

We get the following output:

2
2

The Python @staticmethod

通常,有很多情况下一些函数与类相关,但不需要任何类或实例变量就可以实现一些功能.

比如设置环境变量,修改另一个类的属性等等.这种情况下,我们也可以使用一个函数,一样会将代码扩散到类之外(难以维护)

下面是一个例子:

IND = 'ON'

def checkind():
    return (IND == 'ON')

class Kls(object):
    def __init__(self,data):
        self.data = data

    def do_reset(self):
        if checkind():
            print('Reset done for:', self.data)

    def set_db(self):
        if checkind():
            self.db = 'new db connection'
            print('DB connection made for:',self.data)

ik1 = Kls(12)
ik1.do_reset()
ik1.set_db()

结果:

Reset done for: 12
DB connection made for: 12

现在我们使用@staticmethod, 我们可以将所有代码放到类中

IND = 'ON'

class Kls(object):
    def __init__(self, data):
        self.data = data

    @staticmethod
    def checkind():
        return (IND == 'ON')

    def do_reset(self):
        if self.checkind():
            print('Reset done for:', self.data)

    def set_db(self):
        if self.checkind():
            self.db = 'New db connection'
        print('DB connection made for: ', self.data)

ik1 = Kls(12)
ik1.do_reset()
ik1.set_db()

得到的结果:

Reset done for: 12
DB connection made for: 12

How @staticmethod and @classmethod are different

class Kls(object):
    def __init__(self, data):
        self.data = data

    def printd(self):
        print(self.data)

    @staticmethod
    def smethod(*arg):
        print('Static:', arg)

    @classmethod
    def cmethod(*arg):
        print('Class:', arg)

调用

>>> ik = Kls(23)
>>> ik.printd()
23
>>> ik.smethod()
Static: ()
>>> ik.cmethod()
Class: (<class '__main__.Kls'>,)
>>> Kls.printd()
TypeError: unbound method printd() must be called with Kls instance as first argument (got nothing instead)
>>> Kls.smethod()
Static: ()
>>> Kls.cmethod()
Class: (<class '__main__.Kls'>,)

图解

translate2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`@classmethod` 和 `@staticmethod` 是 Python 用来定义类方法和静态方法的装饰器。 `@classmethod` 装饰器用于定义类方法。类方法是绑定到类而不是实例的方法,可以通过类或实例调用。类方法的第一个参数通常被约定为 `cls`,表示类本身,而不是实例。类方法可以访问类的属性和调用其他类方法。 示例: ```python class MyClass: @classmethod def my_class_method(cls, arg1, arg2): # 类方法可以访问类的属性 print(cls.__name__) print(arg1, arg2) # 通过类调用类方法 MyClass.my_class_method('hello', 'world') # 输出: # MyClass # hello world # 也可以通过实例调用类方法 obj = MyClass() obj.my_class_method('hello', 'world') # 输出: # MyClass # hello world ``` `@staticmethod` 装饰器用于定义静态方法。静态方法不需要访问实例或类的状态,因此不需要传递 `self` 或 `cls` 参数。静态方法可以通过类或实例直接调用。 示例: ```python class MyClass: @staticmethod def my_static_method(arg1, arg2): print(arg1, arg2) # 通过类调用静态方法 MyClass.my_static_method('hello', 'world') # 输出: # hello world # 也可以通过实例调用静态方法 obj = MyClass() obj.my_static_method('hello', 'world') # 输出: # hello world ``` 总结: - `@classmethod` 用于定义类方法,第一个参数为类本身,可以访问类的属性和调用其他类方法。 - `@staticmethod` 用于定义静态方法,不需要访问实例或类的状态,可以通过类或实例直接调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值