Module function vs staticmethod vs classmethod vs no decorators: Which idiom is more pythonic?

前端 未结 4 1451
既然无缘
既然无缘 2020-11-29 18:35

I\'m a Java developer who\'s toyed around with Python on and off. I recently stumbled upon this article which mentions common mistakes Java programmers make when they pick u

4条回答
  •  情话喂你
    2020-11-29 18:57

    Great answer by BrenBarn, but I would change 'If it doesn't need access to the class or the instance, make it a function' to:

    'If it doesn't need access to the class or the instance...but is thematically related to the class (typical example: helper functions and conversion functions used by other class methods or used by alternate constructors), then use staticmethod

    else make it a module function

提交回复
热议问题