Nearby lessons
105 of 159Python - 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.
Example
Static methods are commonly written inside a math or utility class.
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.
Important Points
- Use the
@staticmethoddecorator. - No implicit
selforclsparameter. - Can be called using the class name or object reference.
Summary
| Term | Meaning |
|---|---|
| @staticmethod | Defines a static method |
| self | Not required |
| cls | Not 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 QuestionsProgress: 0 / 6