python class design (staticmethod vs method)

后端 未结 3 1140
轻奢々
轻奢々 2021-01-12 10:40

What\'s the nicer way for methods that don\'t need any passed information (object instance or class) because for example they just do a simple conversion. @staticmet

3条回答
  •  没有蜡笔的小新
    2021-01-12 11:17

    My general rule, for all Object Oriented design, is that if a class has no state, consider using static methods. If the class has state, use instance methods - hands down.

    Furthermore, if the class indeed has no state, reflect for a few minutes and see if you cannot move the static behavior closer to the data and thereby eliminate those static methods with an Extract Method refactoring.

提交回复
热议问题