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).

Community
  • 1
  • 1
mister.elastic
  • 389
  • 2
  • 18
  • 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. – Byte Commander May 02 '16 at 12:11

2 Answers2

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.

Webucator
  • 2,397
  • 24
  • 39
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.

sc3w
  • 1,154
  • 9
  • 21
  • Thanks for the remark about private and public methods. Hence, I clarified the question. – mister.elastic May 02 '16 at 12:06
  • 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, – sc3w May 02 '16 at 12:11