在Python

时间:2017-03-07 20:26:48

标签: python python-2.7 python-3.x

我是Python的新手,但是我爱抚了一个代码,它更像是一个类中的封装方法/函数。 IDE强调了我的代码并给了我使我的方法静态的选项,我做了。值得注意的是,他们给了我与普通代码相同的输出。

我的正常代码

class firstClass():
    def method1(self):
        print "My first method defined in a Class"

    def method2(self, somestring):
        print "My second method in a Class: " + somestring


def main(c=firstClass()):
    c.method1()
    c.method2("somestring")


if __name__ == '__main__':
    main()

我的IDE编辑代码

class firstClass():
    @staticmethod  # What is the meaning of this line really?
    def method1():
        print "My first method defined in a Class"

    @staticmethod
    def method2(somestring):
        print "My second method in a Class" + somestring, "\n"


def main(c=firstClass()):
    c.method1()
    c.method2("somestring")


if __name__ == '__main__':
    main()

我发现当IDE将方法编辑到static时,它确实在我的方法之前传递了@staticmethod,并删除了传递给方法的self参数。但我不明白Python中的静态方法是什么。在Python中使方法静态是什么意思?

0 个答案:

没有答案