• 技术文章 >Python技术 >Python基础教程

    python静态方法和类方法的区别有哪些

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-09-23 17:19:21原创15383

    区别:

    静态方法装饰器下定义的方法属于函数(function);

    类方法装饰器下定义的方法属于方法(method);

    静态方法无需传入任何参数;

    类方法传入的第一个参数必须是class本身cls;

    静态方法与类方法一旦被调用,内存地址即确定。通过类调用和通过实例化对象调用的结果完全一样。

    相关推荐:《Python教程

    直接上代码:

    #  coding:utf-8
    class Apple:
        def fun1(self):
            return 'normal'
        @staticmethod
        def fun2():
            return 'staticmethod'
        @classmethod
        def fun3(cls):
            return 'classmethod'
    print Apple.fun1
    print Apple.fun2
    print Apple.fun3
    print "-"*80
    apple = Apple()
    print apple.fun1
    print apple.fun2
    print apple.fun3
    print "-"*80
    apple1 = Apple()
    print apple1.fun1
    print apple1.fun2
    print apple1.fun3

    运行结果:

    <unbound method Apple.fun1>
    <function fun2 at 0x00000000022FC4A8>
    <bound method classobj.fun3 of <class __main__.Apple at 0x0000000001E7C768>>
    --------------------------------------------------------------------------------
    <bound method Apple.fun1 of <__main__.Apple instance at 0x00000000022FAE08>>
    <function fun2 at 0x00000000022FC4A8>
    <bound method classobj.fun3 of <class __main__.Apple at 0x0000000001E7C768>>
    --------------------------------------------------------------------------------
    <bound method Apple.fun1 of <__main__.Apple instance at 0x00000000022FAE48>>
    <function fun2 at 0x00000000022FC4A8>
    <bound method classobj.fun3 of <class __main__.Apple at 0x0000000001E7C768>>

    普通方法传入的第一个参数必须是self(当然也可以不用self,官方要求尽量用self),self是指实例对象本身;静态方法无需传参;

    类方法传入的第一个参数必须是class,是指类本身。

    对比结果1,5,9行

    fun1通过class调用时,它是未绑定的方法,而实例化apple和apple1之后,它属于绑定的方法,且实例化后的apple和apple1内存地址不同,因为它们属于不同的实例对象。

    对比结果2,6,10行

    静态方法fun2通过class调用或者通过实例化后的对象调用,是没有任何区别的,全部都是指向同一块内存地址。可以简单的理解成静态方法与类或者实例没有任何关系,一旦调用后,它的内存地址即确定。

    对比结果3,7,11行

    类方法fun3通过class调用或者通过实例化后的对象调用,是没有任何区别的,全部都是指向同一块内存地址。为什么?因为实例化对象apple和apple1调用类方法fun3传入的第一个参数是类本身Apple,也就是说apple.fun3 = apple1.fun3 = Apple.fun3。

    专题推荐:python 静态方法 类方法 区别
    上一篇:centos如何安装python2.7 下一篇:python2.x和3.x的区别有哪些

    相关文章推荐

    • Python静态方法和类方法的区别和应用• python里如何调用类方法• python怎么调用类方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网