Why you need to use @staticmethod in your Python code and the benefits of it.

Felipe F Garcia
The Dev Project
Published in
4 min readNov 28, 2021

--

One of the benefits of Python is indeed its flexibility when coding because is such a simple language it allows you to code it in so many different ways.

But knowing the details of Python can improve your code so much, not only the clarity but the usage of your code!

Photo by Mikhail Nilov from Pexels

Python has many decorators that have the intention of doing exactly this, improve your code, make it more readable, maintainable, and reusable, let’s see how the @staticdecorator can help us with it.

How we can improve our Python code?

Ok, what we want to is to be able to use functions that either can be accessed using an instance, or we can access it directly without the need to instantiate that object, let’s use this sample code:

class Calendar:

def __init__(self, date):
self.year = date.year
self.month = date.month
self.day = date.day
self.weekday = date.weekday()


def is_leap_year(self):
return self.year % 400 == 0 or
(self.year % 4 == 0 and self.year % 100 != 0)


def is_weekend(self):
return self.weekday > 4

Here we have a calendar object, and a calendar would need to use a date to be able to fulfill…

--

--

The Dev Project
The Dev Project

Published in The Dev Project

The Dev Project is a journey that will never stop, it’s fun and with a lot of challenges, in this publication space you will find many pieces of information about the Development World.

Felipe F Garcia
Felipe F Garcia

Written by Felipe F Garcia

Backend Engineer 💻 Java 👾 Python 🐍 Top Writer in Medium at the topic Technology 🚀 With a mindset to help and share knowledge 📖.