Nearby lessons

105 of 159

Python - Static Methods

📌 What You Will Learn
  • Understand what a static method is
  • Use the @staticmethod decorator
  • Know that static methods do not require self or cls
  • Call static methods using the class name or an object reference

What Is a Static Method?

A static method is a method that does not use self or cls.

It is usually used for utility operations that do not depend on object or class data.

Syntax

The @staticmethod decorator tells Python that the method belongs to the class but does not require an object or class reference.

Loading editor…

Example

Static methods are commonly written inside a math or utility class.

Loading editor…

Calling Through Object

Although it is recommended to call a static method using the class name, it can also be called through an object reference.

Loading editor…

Important Points

  • Use the @staticmethod decorator.
  • No implicit self or cls parameter.
  • Can be called using the class name or object reference.

Summary

Term Meaning
@staticmethodDefines a static method
selfNot required
clsNot required
📝 Key Takeaways
  • A static method is a method that does not use self or cls
  • It is defined using the @staticmethod decorator
  • It is usually used for general utility operations
  • It can be called using the class name or an object reference

🧠 Test Your Knowledge

6 Questions
Progress: 0 / 6

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.