Have you ever used datetime.datetime.now() method, let's try it now
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2025, 10, 26, 5, 42, 8, 13768)
It's a simple method that returns current date and time.
Notice that we didn’t create an instance of the datetime class. Instead, we imported the datetime module, accessed the datetime class within it, and called it's now() method directly.
We didn’t need to create an object, worry about memory allocation, or manage any class internals. We just used the functionality the class provides.
Methods like now() are known as class methods — they can be called directly on the class itself, without needing to instantiate an object first.
Why Class Methods Matter
Class methods are especially useful when you want to perform operations that relate to the class as a whole rather than a specific object.
For example, if you’re writing software that consumes REST APIs, you can define operational classes with methods that receive a request, make an API call, and return the response or processed result.
A Practical Example: Fetching Dad Jokes 😄
Let's create a class with class methods and it will become clearer.
import requests
class DadJokes:
@classmethod
def get_joke(cls):
response = requests.get('https://icanhazdadjoke.com/', headers={'Accept': 'application/json'})
resp_json = response.json()
return resp_json['joke']
if __name__ == "__main__":
print(DadJokes.get_joke())
get_joke(cls) method in this class is like any other method, to make it a class instead of object method, we have proceeded it with a decorator @classmethod and instead of passing the object it takes an instance of class - we are calling this class instance cls, it can be called anything you like but calling it cls is convention in Python.
The ease of use class methods provide is evident here, we can just reference the class and method and directly call it in print method and it will print a joke for us.
Save this code in a .py file and every time you run it you can get a joke printed.
Note: You’ll need the requests library installed. If you don’t already have it, install it using:
$ pip3 install requests
It’s a small but very handy library for working with REST APIs.
Class Variables in Python
Just like class methods, we can also define class variables.
Class variables belong to the class itself and are not tied to any particular object.
Let’s extend our previous example to include a class variable and see how it works:
import requests
class DadJokes:
api_calls = 0
@classmethod
def get_joke(cls):
response = requests.get('https://icanhazdadjoke.com/', headers={'Accept': 'application/json'})
cls.api_calls += 1
resp_json = response.json()
return resp_json['joke']
if __name__ == "__main__":
print(DadJokes.get_joke())
print(DadJokes.get_joke())
print(DadJokes.get_joke())
print(f"API was called {DadJokes.api_calls}")
We have extended our class to include a new variable api_calls which is initially set to zero. Each time we make an API call in get_joke(cls) method we increase it by 1.
This is one of the simplest ways to track how many times the API has been called.
Notice that Object variables are defined when object is instantiated using __init__ using self.variable_name = variable_value within __init__ method but as class variables don't belong to any object - they are not defined within __init__ instead they are defined as global variables within the class, this can be anywhere in the class but it is a good idea to define them right after the class name before any other methods.
You can access class variables in two ways:
- Using
cls.variable_nameinside a class method (whereclsrefers to the class) - like we are doing in our example, or - By referencing them directly with the class name, e.g.
DadJokes.api_calls.
Output
script@4c14e2993b19:~/temporary$ python3 cmethod_example.py
How do you make a 'one' disappear? You add a 'g' and it's 'gone'
The best time on a clock is 6:30--hands down.
I went to the zoo yesterday and saw a baguette in a cage. It was bread in captivity.
API was called 3
What’s Next
Thanks for stopping by! If you enjoyed this post, join me in the next one. Starting next time, we’ll begin a small project that applies OOP concepts to build classes for analyzing device logs — using logs generated by an IOS XE router running on a CSR platform.