Static methods let you group functions inside a class that don't need to use the class or object data. They help keep related code organized.
Static methods behavior in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
class ClassName: @staticmethod def method_name(parameters): # code here
The @staticmethod decorator marks the method as static.
Static methods do not receive self or cls parameters.
Examples
add that adds two numbers without needing any class or instance data.Python
class MathUtils: @staticmethod def add(a, b): return a + b
log that prints a message. You can call it without creating a Logger object.Python
class Logger: @staticmethod def log(message): print(f"Log: {message}")
Sample Program
This program shows how to define and call a static method multiply inside the Calculator class. We call it directly using the class name.
Python
class Calculator: @staticmethod def multiply(x, y): return x * y # Call static method without creating an object result = Calculator.multiply(4, 5) print(f"4 times 5 is {result}")
Important Notes
Static methods cannot access or modify instance (self) or class (cls) variables.
They behave like regular functions but live inside the class namespace.
You can call static methods using the class name or an instance, but using the class name is clearer.
Summary
Static methods are functions inside classes that don't use instance or class data.
Use @staticmethod decorator to create them.
Call static methods directly on the class without creating objects.
Practice
1. What is a key characteristic of a
staticmethod in Python?easy
Solution
Step 1: Understand the role of static methods
Static methods do not take the instance (self) or class (cls) as their first argument.Step 2: Compare options with static method behavior
It does not receive the instance or class as the first argument. correctly states this key feature, while others describe instance or class methods.Final Answer:
It does not receive the instance or class as the first argument. -> Option AQuick Check:
Static method = no self or cls [OK]
Hint: Static methods don't get self or cls automatically [OK]
Common Mistakes:
- Thinking static methods receive 'self' or 'cls'
- Confusing static methods with instance methods
- Believing static methods modify class state
2. Which of the following is the correct way to define a static method in a Python class?
easy
Solution
Step 1: Identify the decorator for static methods
The@staticmethoddecorator is used to define static methods.Step 2: Check method signature
Static methods do not takeselforclsas parameters, so the method should have no parameters.Final Answer:
@staticmethod\ndef method(): pass -> Option CQuick Check:
@staticmethod + no self/cls = correct static method [OK]
Hint: Use @staticmethod decorator and no parameters [OK]
Common Mistakes:
- Forgetting @staticmethod decorator
- Including self or cls as parameters
- Confusing with @classmethod
3. What will be the output of the following code?
class MyClass:
@staticmethod
def greet():
return "Hello!"
print(MyClass.greet())medium
Solution
Step 1: Understand static method call
Static methods can be called directly on the class without creating an instance.Step 2: Check method return value
The greet method returns the string "Hello!", so printing it outputs "Hello!".Final Answer:
"Hello!" -> Option DQuick Check:
Static method call returns "Hello!" [OK]
Hint: Call static methods on class directly to get return value [OK]
Common Mistakes:
- Expecting an instance to call static method
- Confusing static method with instance method needing self
- Misreading the output as an error
4. Identify the error in this code snippet:
class Example:
@staticmethod
def show(self):
print("Static method")
Example.show()medium
Solution
Step 1: Check static method parameters
Static methods should not have 'self' or 'cls' parameters because they are not passed automatically.Step 2: Understand the call behavior
Calling Example.show() passes no arguments, but method expects one ('self'), causing TypeError.Final Answer:
TypeError because static method has 'self' parameter -> Option AQuick Check:
Static method with 'self' causes TypeError [OK]
Hint: Static methods must not have self or cls parameters [OK]
Common Mistakes:
- Adding self parameter to static methods
- Assuming static methods get instance automatically
- Confusing static and instance methods
5. Given this class:
class Calculator:
@staticmethod
def add(a, b):
return a + b
@staticmethod
def multiply(a, b):
return a * b
# Which call is correct to get the product of 3 and 4?hard
Solution
Step 1: Understand static method parameters
Static methods require all parameters to be passed explicitly since no self or cls is passed.Step 2: Check each call for correctness
Calculator.multiply(3, 4) correctly passes two arguments (3 and 4). Options B, C, and D have wrong or missing arguments.Final Answer:
Calculator.multiply(3, 4) -> Option BQuick Check:
Static method call with correct arguments works [OK]
Hint: Pass all arguments explicitly to static methods [OK]
Common Mistakes:
- Passing self to static methods
- Calling without required arguments
- Confusing static with instance method calls
