@staticmethod or def function()?

Peter Otten __peter__ at web.de
Wed Oct 31 12:55:47 EDT 2018


Tobiah wrote:

> My IDE (pycharm) suggests that I mark my class methods
> with @staticmethod when they don't use 'self'.  Sounds
> good.  I did that then it suggested I had the option
> to make a regular function instead, at the file level.
> That's a possibility.  I was thinking that I'd leave
> the method in the class unless it was ever also used
> by another class.  What do you think?

What do *you* think? Is your code demonstrably better if you turn the 
"normal" method into a static method?

Do not make changes to your code only to apeace a linter.

Regarding more the general question, should I use an instance method, a 
class method, a static method, or a function? -- that is hard to answer 
without an idea what the specific task of the function/method is, and how 
strong the link to the class is.




More information about the Python-list mailing list