Static methods are similar to instance methods (class-level methods), except static methods behave like regular functions bound to a class namespace. Unlike instance methods, static methods do not have access to cls
or self
.
Creating static methods
A class named StringUtil
contains a is_empty
method for determining if a string is empty.
|
|
When to use static methods
Static methods are advantageous for:
- Singletons
- Utility classes with similar grouped methods
- Methods that belong in a class namespace, but do not require instance variables (
self
)