Avatar ·

为什么Python需要@staticmethod

📁 装饰师, 方法, python, 人工智能

有一个类Printer。它有两个做同样事情的方法。区别在于其中一个方法带有装饰器@staticmethod,而另一个没有。但我可以在不创建类实例的情况下调用这两个方法。

class Printer(): ''''Test @staticmethod'''' def not_static_print(self, text='Example Text'): print(text) @staticmethod def static_print(text='Example Text'): print(text) # '感觉像这样。'

对于not_static_print(),我指定了一个实例,或者