2

I know it is good style in other languages (C#, JAVA, ...) to make "private" methods (using two underscores) static if possible. Is there any recommendation for Python?

I would rather not make them static, because they are only private (I don't want to move them to utility classes and they are not performance relevant) and I want to avoid cluttering my code with decorators. On the other hand, the static code analyzer tells me to do so (and I want to write code in the "Pythonic" way).

1
  • 1
    My personal opinion on this would be to not use static class methods unless needed, neither private nor public ones. Keep it simple. Unless it's a very small method that gets called vary often and is proved to slow down the entire program significantly, it makes no sense to optimize every tiny bit. Commented May 2, 2016 at 12:11

2 Answers 2

2

The short answer is you should almost never use static methods in Python. They were added by mistake.

I meant to implement class methods but at first I didn't understand them and accidentally implemented static methods first. - Guido van Rossum

You can almost definitely achieve what you want with a class method or an instance method.

More on why static methods in Python are mostly useless here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for this answer (incl. the useful links)!
0

In python there is no such thing like 'private method', all methods are public. The only thing that tells you that a method is not intended to use publicly is that it's name starts with an underscore.

If your methods are not referencing anything from self (an instance of the class) then you could make them static to optimize the memory usage of your application.

2 Comments

Thanks for the remark about private and public methods. Hence, I clarified the question.
It's not very clear what would you like to achieve. If you don't want to make those methods static than ignore the warning from the static code analyzer,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.