Module 11 · Object-Oriented Programming — topic 13 of 25
Static methods
Methods with @staticmethod that receive neither self nor cls: utility functions grouped with a class, validation helpers, and when a plain module function is better.
Learning objectives
- Define and call static methods
- Choose between instance, class and static methods
- Recognise when a module-level function is clearer
Definition
A static method is defined with the @staticmethod decorator and receives no automatic first argument. It behaves like a normal function that lives in the class's namespace.
Introduction
Some helpers relate to a class but don't need any object or class data: checking whether a PIN is well formed, converting units. Static methods keep such helpers next to the class that uses them.
Why it matters
Knowing the three method types — instance, class and static — lets you pick the one that states your intent: 'needs the object', 'needs the class', 'needs neither'.
Detailed explanation
Decorate with @staticmethod; there is no self or cls parameter.
Call through the class (Account.is_valid_pin('1234')) or an instance; both work.
Static methods are useful for validation helpers and pure computations that conceptually belong to the class.
Summary: instance methods use self (object data); class methods use cls (class data or construction); static methods use neither.
If a helper isn't specifically about the class, a module-level function is simpler and easier to reuse.
Syntax
- No self or cls.
- Call on the class or an instance.
- Use for helpers related to the class.
Code examples
A validation helper
1234 True 12a4 False 12345 False
Three kinds of method
25C 0C 77.0
More examples
Static helpers used by instance methods
Rs 1,486.80 Rs 1,234,567.80 bad line: pen
Step-by-step explanation
- valid_line and money need no object data, so they are static.
- The constructor and total call them through the class or self — both work.
- money is also useful on its own, called as Order.money(...).
Expected output
Running “Static helpers used by instance methods” prints exactly:
Practical applications
- Format and parse helpers grouped with a domain class.
- Validation rules checked before creating objects.
Best practices
- Use static methods sparingly for closely related helpers.
- Prefer module functions for general utilities.
- Choose the method type that states what data is needed.
Common mistakes
Accessing self in a static method
Avoid
Prefer
Static methods don't receive the instance; if you need it, use an instance method.
Important notes
- Static methods are common in other languages because they lack module-level functions; in Python, modules often serve that purpose.
Key takeaways
- @staticmethod methods receive no self or cls.
- They group related helpers with a class.
- Instance → object data; class → class data; static → neither.
Try it yourself
- 1Add a static method is_valid_email to a User class and use it in __init__.
- 2Convert an instance method that doesn't use self into a static method.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?