What is the difference between @staticmethod and a module-level function, and when should you prefer one over the other?

Python Professional Hard

Python Professional — Hard

What is the difference between @staticmethod and a module-level function, and when should you prefer one over the other?

Key points

  • @staticmethod is inherited by subclasses
  • Module-level functions are independent of classes
  • @staticmethod is used for logic related to a specific class
  • Module functions are preferred for logic independent of any class

Ready to go further?

Related questions