Python中的method —— static mthod, class method和instance method

来源:互联网 发布:javascript启示录云盘 编辑:程序博客网 时间:2024/06/06 07:03

Python中的method

通常来说,Python中的类(class)可以包含三类方法:@staticmethod@classmethod和没有任何decorator修饰的实例方法。下面我们分别讨论这几种方法之间的区别。

示例代码

class A(object):    def func(self, x):        print('executing func with %s, %s.' %(str(self), str(x)))    @classmethod    def class_func(cls, x):        print('executing class_func with %s, %s.' %(str(cls), str(x)))    @staticmethod    def static_func(x):        print('executing statuc_func with %s.' %(str(x))a = A()

instance method

上面class中的func()就是一个instance method。也就是我们平时最常用的方法。instance method绑定于类的实例,同时instance method隐式接收一个该类的实例作为输入的第一个参数,同时也只能由该类的实例调用。

当我们用A类的实例调用func的时候,实际上func已经是一个部分被应用的方法了,或者说func所接收的self参数实际上已经同该实例绑定了。

如下面的例子所示:

>>> a.func(1)executing func with <__main__.A object at 0x102c1bf90>, 1.>>> A.func(1)TypeError: unbound method func() must be called with A instance as first argument (got int instance instead)# a.func()self参数已经与a绑定,所以下面的异常显示1 given.>>> a.func()TypeError: func() takes exactly 2 arguments (1 given)>>> a.func<bound method A.func of <__main__.A object at 0x102c1bf90>>

class method

所谓的class method,或说类方法,就是指绑定于某一个类的方法,从属于类而非其实例。Python中的的类方法也就是Java中所谓的类方法或静态方法。上面class中的class_func()就是class method。

从语法上看,class method可以由类本身调用,也可以由类的实例调用。但由类实例调用class method从原则上来说违反了class method的本意。所以不推荐使用。

instance method绑定实例类似,class method所接收的cls参数同其所属的类绑定。

如下面例子所示:

>>> A.class_func(1)executing class_func with <class '__main__.A'>, 1.>>> a.class_func(1)executing class_func with <class '__main__.A'>, 1.# A.class_func的第一个参数已经与A绑定,所以下面的异常显示1 given.>>> A.class_func()TypeError: class_func() takes exactly 2 arguments (1 given)>>> A.class_func <bound method type.class_func of <class '__main__.A'>>

static method

static method不与类中的任何元素绑定。static method就如同在python文件中直接定义一个方法一样,不同之处只在于。同class methodinstance method不同的是,static method不接收任何隐式传入的参数(比如class methodclsinstance methodself)。static method可以由类本身或类实例调用。

staticmethod所起的作用主要在于将逻辑上属于该类的方法放在该类中,从而保存面向对象封装的性质。

如下面例子所示:

>>> A.static_func(1)executing statuc_func with 1.>>> a.static_func(1)executing statuc_func with 1.>>> A.static_func<function __main__.static_func>

class method的使用示例:提供额外的构造器

定义一个类A:

class A(object):    def __init__(self, s):        self.s = s    @classmethod    def construct_from_list(cls, str_list):        a = cls('')        a.s = ','.join(str(ele) for ele in str_list)        return a    def __repr__(self):        print('A(%r)' % (self.s))        return('A.object<{}>'.format(self.s)

然后进行如下操作:

>>> a1 = A('1,2,3')>>> a1A('1,2,3')A.object<1,2,3>>>> a2 = A.construct_from_list(range(3))>>> a2A('0,1,2')A.object<0,1,2>

可见我们定义的class method可以作为另一个构造器使用。

同时该构造器在子类中仍然可用:

class B(A):    def __repr__(self):        return 'B<{}>'.format(self.s.upper())

然后进行如下操作:

>>> b = B.construct_from_list(['a', 'b']>>> bB<A,B>

可见该构造器在子类中任然能够调用。

原创粉丝点击