python method in @staticmethod

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Nash_Cyk/article/details/79065770

in python @staticmethod method, similar to the C ++ static, easy to integrate external to the class in the body, mainly the direct method can be accessed without instantiating the class, if you remove staticmethod, the method is also applied in the self examples of access methods can also be integrated.

class Test:
      def __init__(self,num):
            self.num = num;
      def cout_num(self):
            print(self.num)
      @staticmethod
      def print_num():
            print("Hello World")         
if __name__ == "__main__":
      obj = Test(10)
      """实例化成员方法"""
      obj.cout_num()
      """直接访问静态方法"""
      Test.print_num()
      """实例化 访问静态方法"""
      obj.print_num()

The output
10
the Hello World
the Hello World

Guess you like

Origin blog.csdn.net/Nash_Cyk/article/details/79065770