在静态方法中引用自己的类有简写吗?
假设我有这段代码:
class SuperLongClassName(object):
@staticmethod
def sayHi():
print 'Hi'
@staticmethod
def speak():
SuperLongClassName.sayHi() # Is there a shorthand?
最佳答案
是的,使用 @classmethod
而不是 @staticmethod
。 @staticmethod
的重点是在不需要时删除额外的类参数。
class SuperLongClassName(object):
@classmethod
def sayHi(cls):
print 'Hi'
@classmethod
def speak(cls):
cls.sayHi()
关于python - 在静态方法中引用自己的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604316/