Calling static method in python

前端 未结 5 1170
走了就别回头了
走了就别回头了 2020-12-13 01:51

I have a class Person and a static method in that class called call_person:

class Person:
    def call_person():
        print \"he         


        
5条回答
  •  情书的邮戳
    2020-12-13 02:42

    In python 3.x, you can declare a static method as following:

    class Person:
        def call_person():
            print "hello person"
    

    but the method with first parameter as self will be treated as a class method:

    def call_person(self):
        print "hello person"
    

    In python 2.x, you must use a @staticmethod before the static method:

    class Person:
        @staticmethod
        def call_person():
            print "hello person"
    

    and you can also declare static method as:

    class Person:
        @staticmethod
        def call_person(self):
            print "hello person"
    

提交回复
热议问题