HomeData science8 Constructed-in Python Decorators to Write Elegant Code

8 Constructed-in Python Decorators to Write Elegant Code

8 Built-in Python Decorators to Write Elegant Code
Picture by Editor


Techwearclub WW

Python, with its clear and readable syntax, is a extensively used high-level programming language. Python is designed for ease of use that emphasizes simplicity and diminished price of program upkeep. It comes with an in depth library that reduces the necessity for builders to put in writing code from scratch and will increase builders’ productiveness. One highly effective characteristic of Python that contributes to code magnificence is decorators.



In Python, a decorator is a operate that means that you can modify the habits of one other operate with out altering its core logic. It takes one other operate as an argument and returns the operate with prolonged performance. This fashion, you should use decorators so as to add some further logic to current features to extend reusability with just some traces of code. On this article, we are going to discover eight built-in Python decorators that may assist you to write extra elegant and maintainable code.


8 Built-in Python Decorators to Write Elegant Code8 Built-in Python Decorators to Write Elegant Code
Picture by Editor



The @atexit.register decorator is used to register a operate to be executed at program termination. This operate can be utilized to carry out any activity when this system is about to exit, whether or not it’s as a result of regular execution or an sudden error.




import atexit

# Register the exit_handler operate
def exit_handler():
    print("Exiting this system. Cleanup duties might be carried out right here.")

# Remainder of this system
def essential():
    print("Inside the principle operate.")
    # Your program logic goes right here.

if __name__ == "__main__":


Inside the principle operate.
Exiting this system. Cleanup duties might be carried out right here.


Within the above implementation, @atexit.register is talked about above the operate definition. It defines the exit_handler() operate as an exit operate. Basically, it signifies that at any time when this system reaches its termination level, both by means of regular execution or as a result of an sudden error inflicting a untimely exit, the exit_handler() operate might be invoked.



The @dataclasses.dataclass is a robust decorator that’s used to robotically generate frequent particular strategies for lessons corresponding to “__init__”, “__repr__” and others. It helps you write cleaner, extra concise code by eliminating the necessity to write boilerplate strategies for initializing and evaluating situations of your class. It may well additionally assist forestall errors by making certain that frequent particular strategies are carried out persistently throughout your codebase.




from dataclasses import dataclass

class Level:
    x: int
    y: int

level = Level(x=3, y=2)
# Printing object

# Checking for the equality of two objects
point1 = Level(x=1, y=2)
point2 = Level(x=1, y=2)
print(point1 == point2)



The @dataclass decorator, utilized above the Level class definition, alerts Python to make the most of default habits for producing particular strategies. This robotically creates the __init__ technique, which initializes class attributes, corresponding to x and y, upon object instantiation. Because of this, situations like level might be constructed with out the necessity for specific coding. Furthermore, the __repr__ technique, answerable for offering a string illustration of objects, can be robotically adjusted. This ensures that when an object, like a degree, is printed, it yields a transparent and ordered illustration, as seen within the output: Level(x=3, y=2). Moreover, the equality comparability (==) between two situations, point1 and point2, produces True. That is noteworthy as a result of, by default, Python checks for equality primarily based on reminiscence location. Nevertheless, within the context of dataclass objects, equality is set by the information contained inside them. It’s because the @dataclass decorator generates an __eq__ technique that checks for the equality of the information current within the objects, relatively than checking for a similar reminiscence location.



The @enum.distinctive decorator, discovered within the enum module, is used to make sure that the values of all of the members of an enumeration are distinctive. This helps forestall the unintentional creation of a number of enumeration members with the identical worth, which may result in confusion and errors. If duplicate values are discovered, a ValueError is raised.




from enum import Enum, distinctive

class VehicleType(Enum):
    CAR = 1
    TRUCK = 2
    BUS = 4

# Making an attempt to create an enumeration with a reproduction worth will increase a ValueError
    class DuplicateVehicleType(Enum):
        CAR = 1
        TRUCK = 2
        MOTORCYCLE = 3
        # BUS and MOTORCYCLE have duplicate values
        BUS = 3
besides ValueError as e:
    print(f"Error: {e}")



Error: duplicate values present in : BUS -> MOTORCYCLE


Within the above implementation, “BUS” and “MOTORCYCLE” have the identical worth “3”. Because of this, the @distinctive decorator raises a ValueError with a message indicating that duplicate values have been discovered. Neither can you employ the identical key greater than as soon as nor are you able to assign the identical worth to totally different members. On this method, it helps forestall duplicate values for a number of enumeration members.



The partial decorator is a robust software that’s used to create partial features. Partial features mean you can pre-set a few of the arguments of the unique operate and generate a brand new operate with these arguments already crammed in.




from functools import partial

# Authentic operate
def energy(base, exponent):
    return base ** exponent

# Making a partial operate with the exponent mounted to 2
sq. = partial(energy, exponent=2)

# Utilizing the partial operate
consequence = sq.(3)




Within the above implementation, we have now a operate “energy” which accepts two arguments “base” and “exponent” and returns the results of the bottom raised to the ability of exponent. We’ve got created a partial operate named “sq.” utilizing the unique operate during which the exponent is pre-set to 2. On this approach, we will lengthen the performance of authentic features utilizing a partial decorator.



The @singledisptach decorator is used to create generic features. It means that you can outline totally different implementations of features with the identical title however totally different argument varieties. It’s significantly helpful while you need your code to behave in a different way for various knowledge varieties.




from functools import singledispatch

# Decorator
def display_info(arg):
    print(f"Generic: {arg}")

# Registering specialised implementations for various varieties
def display_int(arg):
    print(f"Acquired an integer: {arg}")

def display_float(arg):
    print(f"Acquired a float: {arg}")

def display_str(arg):
    print(f"Acquired a string: {arg}")

def display_sequence(arg):
    print(f"Acquired a sequence: {arg}")

# Utilizing the generic operate with differing kinds
display_info("Good day World!")
display_info([2, 4, 6])     



Acquired an integer: 39
Acquired a float: 3.19
Acquired a string: Good day World!
Acquired a sequence: [2, 4, 6]


Within the above implementation, we first developed the generic operate display_info() utilizing the @singledisptach decorator after which registered its implementation for int, float, string, and record individually. The output exhibits the working of display_info() for separate knowledge varieties.



The @classmethod is a decorator used to outline class strategies inside the class. Class strategies are sure to the category relatively than the article of the category. The first distinction between static strategies and sophistication strategies lies of their interplay with the category state. Class strategies have entry to and may modify the category state, whereas static strategies can’t entry the category state and function independently.




class Pupil:
    total_students = 0

    def __init__(self, title, age):
        self.title = title
        self.age = age
        Pupil.total_students += 1

    def increment_total_students(cls):
        cls.total_students += 1
        print(f"Class technique known as. Whole college students now: {cls.total_students}")

# Creating situations of the category
student1 = Pupil(title="Tom", age=20)
student2 = Pupil(title="Cruise", age=22)

# Calling the category technique
Pupil.increment_total_students()  #Whole college students now: 3

# Accessing the category variable
print(f"Whole college students from scholar 1: {student1.total_students}")
print(f"Whole college students from scholar 2: {student2.total_students}")




Class technique known as. Whole college students now: 3
Whole college students from scholar 1: 3
Whole college students from scholar 2: 3


Within the above implementation, the Pupil class has total_students as a category variable. The @classmethod decorator is used to outline the increment_total_students() class technique to increment the total_students variable. Each time we create an occasion of the Pupil class, the overall variety of college students is incremented by one. We created two situations of the category after which used the category technique to switch the total_students variable to 3, which can be mirrored by the situations of the category.



The @staticmethod decorator is used to outline static strategies inside a category. Static strategies are the strategies that may be known as with out creating an occasion of the category. Static strategies are sometimes used after they do not need to entry object-related parameters and are extra associated to the category as a complete.




class MathOperations:
    def add(x, y):
        return x + y

    def subtract(x, y):
        return x - y

# Utilizing the static strategies with out creating an occasion of the category
sum_result = MathOperations.add(5, 4)
difference_result = MathOperations.subtract(8, 3)

print("Sum:", sum_result)            
print("Distinction:", difference_result)




Within the above implementation, we have now used @staticmethod to outline a static technique add() for the category “MathOperations”. We’ve got added the 2 numbers “4” and “5” which leads to “9” with out creating any occasion of the category. Equally, subtract the 2 numbers “8” and “3” to get “5”. This fashion static strategies might be generated to carry out utility features that don’t require the state of an occasion. 



The @property decorator is used to outline the getter strategies for the category attribute. The getter strategies are the strategies that return the worth of an attribute. These strategies are used for knowledge encapsulation which specifies who can entry the small print of the category or occasion.




class Circle:
    def __init__(self, radius):
        self._radius = radius

    def radius(self):
        # Getter technique for the radius.
        return self._radius

    def space(self):
        # Getter technique for the realm.
        return 3.14 * self._radius**2

# Creating an occasion of the Circle class
my_circle = Circle(radius=5)

# Accessing properties utilizing the @property decorator
print("Radius:", my_circle.radius)          
print("Space:", my_circle.space)  




Within the above implementation, the category “Circle” has an attribute “radius”. We’ve got used @property to arrange the getter strategies for the radius in addition to the realm. It gives a clear and constant interface for the customers of the category to entry these attributes. 



This text highlights a few of the most versatile and useful decorators that you should use to make your code extra versatile and readable. These decorators allow you to lengthen the functionalities of the unique operate to make it extra organized and fewer vulnerable to errors. They’re like magic touches that make your Python packages look neat and work easily.

Kanwal Mehreen is an aspiring software program developer with a eager curiosity in knowledge science and functions of AI in medication. Kanwal was chosen because the Google Era Scholar 2022 for the APAC area. Kanwal likes to share technical information by writing articles on trending matters, and is obsessed with bettering the illustration of girls in tech trade.

Supply hyperlink

Opinion World [CPL] IN

latest articles

explore more