@classmethod在Python中的用途是什么?

问题描述

我目前正在学习Python。在OOP中,@classmethod装饰器的用途是什么?通过@classmethod访问类变量并仅通过常规方法访问它有什么区别?就像我可以这样做一样:

class A():
   _variable = A

   def getVariable(self):
      return self._variable 
 

class A():
   _variable = A

   @classmethod
   def getVariable(cls):
      return cls._variable 
 

两种方法有什么区别?为什么在什么时候使用classmethod以及什么时候不需要使用classmethod?如果我不这样做会怎样?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)