Python类方法和静态方法


python没有和C++中static关键字,它的静态方法是怎样的呢?还有其它语言中少有的类方法又是神马?

python中实现静态方法和类方法都是依赖于python的修饰器来实现的。

[python]
  1. class MyClass:  
  2.   
  3.     def  method(self):  
  4.            print("method")  
  5.  
  6.     @staticmethod  
  7.     def  staticMethod():  
  8.             print("static method")  
  9.  
  10.      @classmethod  
  11.      def classMethod(cls):  
  12.            print("class method")  

大家注意到普通的对象方法、类方法和静态方法的去别了吗?

对象方法有self参数,类方法有cls参数,静态方法是不需要这些附加参数的。

在C++中是没有类方法着个概念的的。

相关内容