模仿静态方法
这个问题在这里已经有了答案:
你应该使用@classmethod :
@classmethod
def show(cls, message):
print("The message is: {}".format(message))
classmethod和staticmethod之间的区别在于后者对它的封闭类一无所知,而前者不会(通过cls参数)。 一种staticmethod可以在课堂外轻松宣布。
如果你不想让show()知道关于C任何信息,可以在C之外使用@staticmethod或声明show() 。
来自其他语言的静态方法的惯用翻译通常是模块级别的方法。
def show(message):
print("The message is: {}".format(message))
告诉你python有@staticmethod的答案是正确的,并且也有误导性:只使用模块级函数通常是正确的。
你应该使用@staticmethod :
@staticmethod
def show(message):
print("The message is: {}".format(message))
链接地址: http://www.djcxy.com/p/55133.html
上一篇: Imitating a static method
下一篇: Call function without creating an instance of class first
