我已阅读在Python中使用@staticmethod和全局函数有什么区别?
- What is the difference between @staticmethod and @classmethod in Python?
- Python @classmethod and @staticmethod for beginner?
由于staticmethod
不能访问类的实例,我不知道什么是betweent并global function
区别?
何时应该使用staticmethod
?可以举个很好的例子吗?
我opion最好的答案是你的第一个链接的批准答案顶部评论:“一个静态方法并非一无是处 - 这是一种将函数放入类中的方法(因为它在逻辑上属于该类),同时指示它不需要访问该类。“你可以想像'datetime'类具有特定日期相关事物的静态方法,但这些函数也可能在模块级别。 – Evert 2013-02-20 09:23:20