Remember
A staticmethod receives neither an implicit instance nor an implicit class. Its arguments come from the call.
A staticmethod receives neither an implicit instance nor an implicit class. Its arguments come from the call.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Math:
@staticmethod
def double(number):
return number * 2
print(Math.double(3))Top-level scope
class MathA staticmethod receives neither an implicit instance nor an implicit class. Its arguments come from the call.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Math:
@staticmethod
def double(number):
return number * 2
print(Math.double(3))
|
v
DISPLAYED RESULT
6
FINAL BINDINGS
Math --> object 2 (type)
object 2: class MathChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Math:
@staticmethod
def double(number):
return number * 2
print(Math.double(3))Top-level scope
class MathThe example has finished. Compare the final output with the values and references above.
6
What do you expect this program to print?
class Math:
@staticmethod
def double(number):
return number * 2
print(Math.double(3))staticmethod() FunctionThe staticmethod() function in Python is used to define a static method inside a class. A static method doesn't take the instance (self) or class (cls) as the first argument. It behaves like a regular function but belongs to the class’s namespace.
staticmethod(function)
function – A function you want to convert into a static method.class MathUtils:
@staticmethod
def add(a, b):
return a + b
# Call without creating an object
print(MathUtils.add(5, 3))
8
You can also use staticmethod() without the @staticmethod decorator:
class MathUtils:
def add(a, b):
return a + b
add = staticmethod(add)
print(MathUtils.add(10, 4))
14
| Feature | staticmethod() | classmethod() |
|---|---|---|
| Access to instance? | No | No |
| Access to class? | No | Yes |
| First argument | None | cls |
| Use case | Helper functions | Factory methods |
self or cls inside a static method – it won’t work.@staticmethod decorator or staticmethod() function – method acts as a normal one and expects self.Interviewers often ask the difference between staticmethod() and classmethod(). Know when and why to use each.
staticmethod() makes a function callable via the class without needing an object.Create a class Temperature with a static method celsius_to_fahrenheit(c) that converts Celsius to Fahrenheit.
class Temperature:
@staticmethod
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
print(Temperature.celsius_to_fahrenheit(37))
98.6
Connect what you noticed in the example with the idea below.
A staticmethod receives neither an implicit instance nor an implicit class. Its arguments come from the call.
A staticmethod always receives self automatically.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.