Decorators and Generators in Python

8/16/2025

#Decorars Generators in Python

Go Back

Decorators and Generators in Python: A Complete Guide with Examples

Python is known for its simplicity and flexibility. Two powerful features that make Python stand out are Decorators and Generators. These concepts help developers write cleaner, more efficient, and reusable code. In this article, we’ll explore what they are, how they work, and provide examples to help you master them.


#Decorars  Generators in Python

What are Decorators in Python?

A decorator in Python is a special function that modifies the behavior of another function or class without changing its code. Decorators are widely used in logging, authentication, caching, and framework development (e.g., Django, Flask).

βœ… Example of a Decorator

def my_decorator(func):
    def wrapper():
        print("Before the function runs")
        func()
        print("After the function runs")
    return wrapper

@my_decorator
def say_hello():
    print("Hello, World!")

say_hello()

Output:

Before the function runs
Hello, World!
After the function runs

πŸ‘‰ Here, the @my_decorator syntax applies the decorator to say_hello().


πŸ“Œ Use Cases of Decorators

  • Logging function calls

  • Checking user authentication

  • Measuring execution time

  • Enforcing access control

  • Code reusability and cleaner syntax


πŸ”Ή What are Generators in Python?

A generator in Python is a function that returns an iterator using the yield keyword. Unlike normal functions that return a single value, generators yield multiple values one at a time and maintain their state between calls.

This makes them memory-efficient and ideal for handling large datasets or infinite sequences.

βœ… Example of a Generator

def count_up_to(n):
    count = 1
    while count <= n:
        yield count
        count += 1

for number in count_up_to(5):
    print(number)

Output:

1
2
3
4
5

πŸ‘‰ Here, yield allows the function to return values one at a time without storing the entire sequence in memory.


πŸ“Œ Advantages of Generators

  • Memory Efficiency β†’ Generate data on the fly instead of storing it.

  • Infinite Sequences β†’ Useful for streams and pipelines.

  • Improved Performance β†’ Faster for large-scale data processing.


πŸ”„ Key Differences Between Decorators and Generators

FeatureDecoratorsGenerators
PurposeModify behavior of functions or classesProduce values one at a time
Keyword Used@decorator syntaxyield keyword
Use CaseLogging, caching, authenticationIteration, handling large datasets
Return TypeFunctionIterator

Β Final Thoughts

Both decorators and generators are advanced features that make Python programming more powerful.

  • Use decorators when you need to extend or modify function behavior without rewriting code.

  • Use generators when working with large data streams to save memory and improve performance.

By mastering these, you can write more efficient, reusable, and Pythonic code.

Table of content