python python @classmethod @staticmethod和实例方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python python @classmethod @staticmethod和实例方法相关的知识,希望对你有一定的参考价值。


class Foo(object):

    variable = 1

    def method(self, param):
        print "run instance method (%s,%s)" % (self, param)

    @classmethod
    def class_method1(cls, param):
        print "run class method (%s,%s)" % (cls, param)

    @classmethod
    def class_method2(cls):
        print cls.variable

    @staticmethod
    def static_method1(param):
        print "run static method (%s)" % param

    @staticmethod
    def static_method2():
        print variable

foo = Foo()

foo.method("instance method")
#Foo.method("instance method again")

foo.class_method1("class method 1")
foo.class_method2()
Foo.class_method1("class method 1 again")
Foo.class_method2()

foo.static_method1("static method 1")
foo.static_method2()
Foo.static_method1("static method 1 again")
Foo.static_method2()


以上是关于python python @classmethod @staticmethod和实例方法的主要内容,如果未能解决你的问题,请参考以下文章

代写python,代写python编程,python代写,python编程代写,留学生python代写

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍