MCA
DECORATORS & GENERATORS // MASTER THE CONCEPTS // WRITE BRUTAL CODE // DECORATORS & GENERATORS // MASTER THE CONCEPTS // WRITE BRUTAL CODE //
BACK TO SYLLABUS
HARD

DECORATORS & GENERATORS

Advanced function concepts and iterators

CONCEPTS

01Higher-order functions
02Function decorators
03Decorators with arguments
04Yield keyword
05Generators vs iterables
06Generator expressions

SYNTAX_DEMO

Metaprogramming and lazy evaluation
# Decorator
def uppercase_decorator(func):
    def wrapper():
        result = func()
        return result.upper()
    return wrapper

@uppercase_decorator
def greet():
    return "hello world"

print(greet())

# Generator
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for num in countdown(3):
    print(num)