When to use Static Methods in Python? Never

See Python: Tips and Tricks for similar articles.
Looking to improve your Python skills? Check out Webucator’s Python classes.

The short answer is in the title. Never use static methods in Python.

The initial intention of this post was to provide a simple and concrete use case for a static method in Python. I started with this 7-year conversation about static methods on StackOverflow, which seemed promising. But it doesn’t contain a single practical use case. And I think I know why. There are none.

Creating a static method is straightforward enough, but as Nathan Tregillus sort of asks in this post, the real question is why would you ever want to?

In answer to a similar question about when to use static methods in Java, Associate Prof not-just-yeti wrote this short-and-sweet explanation:

One rule-of-thumb: ask yourself “does it make sense to call this method, even if no Obj has been constructed yet?” If so, it should definitely be static.

Initially, I thought that made sense. But, assuming you have decided to use an object-oriented rather than a procedural approach to coding, why would you ever want to call a method before instantiating an object?

Isn’t obj.method() always better than Class.method(obj)?

Or to give a more concrete example, wouldn’t you rather write -5.abs() than Math.abs(-5) as Miško Hevery suggest in Static Methods are Death to Testability?

Multiple Arguments

In Java, I can see how it is helpful to call a method on a class when you want to pass in multiple arguments. For example, Math.max(5, 7). But why does this need to be a static method? In Python, you could just make it a class method, only callable on the class itself and not on instances of a class.

The Math class in Java is basically just a grouping of math-related utility methods. In Python, this is generally handled via modules (e.g., the math module), not classes.

Incidentally, in this discussion, Google Senior Software Developer and author of C# in Depth, Jon Skeet explains why he thinks static methods in Java should have been designed to behave like class methods in Python do and should not be callable on instance objects.

Built-in Static Methods in Python

It’s hard to find an example of a practical use of a static method in Python. I found two examples of static methods built in to Python:

  1. str.maketrans() – I cannot understand why this is a static rather than a class method. The str class object is always available in Python, so why would you want to call maketrans() on an instance of str?
  2. importlib.abc.InspectLoader.source_to_code() – This was made static in Python 3.5 to “make it easier to work with source code in a string“.

I’m sure there are other examples, but I bet they are all equally obscure and not worth the time it takes to type “@staticmethod”.

So, are static methods completely useless in Python? Guido van Rossum basically said as much in April, 2012:

We all know how limited static methods are. (They’re basically an accident — back in the Python 2.2 days when I was inventing new-style classes and descriptors, I meant to implement class methods but at first I didn’t understand them and accidentally implemented static methods first. Then it was too late to remove them and only provide class methods.

So, if you’re wondering if you should make one of your Python methods static, as far as I can tell, you shouldn’t do it. Ever. Right?

Written by Nat Dunn.

Related Articles

  1. Scientific Notation in Python
  2. Understanding Python’s __main__ variable
  3. Associate Python Files with IDLE
  4. Python: isdigit() vs. isdecimal()
  5. Python Color Constants Module
  6. Python: pow(x, y, z) less efficient than x**y % z
  7. A Python Model for Ping Pong Matches
  8. Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)
  9. Collatz Conjecture in Python
  10. Finally, a use case for finally – Python Exception Handling
  11. Python Clocks Explained
  12. Python’s date.strftime() slower than str(), split, unpack, and concatenate?
  13. Bi-directional Dictionary in Python
  14. Maximum recursion depth exceeded while calling a Python object
  15. Basic Python Programming Exercise: A Penny Doubled Every Day
  16. Creating an Email Decorator with Python and AWS
  17. How to Create a Simple Simulation in Python – Numeric Data
  18. Python Coding Challenge: Two People with the Same Birthday
  19. How to find all your Python installations on Windows
  20. Change Default autosave Interval in JupyterLab
  21. Interactive Quiz using IPython Notebook
  22. When to use Static Methods in Python? Never (this article)
  23. Converting Leading Tabs to Spaces with Python
  24. Simple Python Script for Extracting Text from an SRT File
  25. Python Virtual Environments with venv
  26. Mapping python to Python 3 on Your Mac
  27. How to Make IDLE the Default Editor for Python Files on Windows
  28. How to Do Ternary Operator Assignment in Python
  29. How to Convert Seconds to Years with Python
  30. How to Create a Python Package
  31. How to Read a File with Python
  32. How to Check the Operating System with Python
  33. How to Use enumerate() to Print a Numbered List in Python
  34. How to Repeatedly Append to a String in Python
  35. Checking your Sitemap for Broken Links with Python
  36. How to do Simultaneous Assignment in Python
  37. Visual Studio Code - Opening Files with Python open()
  38. How to Slice Strings in Python
  39. How Python Finds Imported Modules
  40. How to Merge Dictionaries in Python
  41. How to Index Strings in Python
  42. How to Create a Tuple in Python