week 4

Week 4: Functional Programming & System Modules


1. Decorators (Function Wrappers)

1. Basic Decorator

def decorator(func):
    def wrapper():
        print("Before function call")
        func()
        print("After function call")
    return wrapper

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

say_hello()

2. Decorator with Arguments

def repeat(n):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(n):
                func(*args, **kwargs)
        return wrapper
    return decorator

@repeat(3)
def greet():
    print("Hi!")

greet()

3. Logging Decorator

4. Timer Decorator


2. Generators & Iterators

5. Basic Generator

6. Generator for Fibonacci Series

7. Infinite Generator

8. Custom Iterator


3. Threading & Multiprocessing

9. Basic Threading

10. Threading with Delay

11. Multiprocessing Example

12. Shared Memory (Multiprocessing Manager)


4. Design Patterns

Singleton Pattern

13. Singleton Class


Factory Pattern

14. Factory Method


Observer Pattern

15. Observer Implementation


5. Useful Python Tricks

16. Swapping Variables in One Line

17. Dictionary Comprehension

18. Merging Two Dictionaries

19. Finding Most Frequent Element in a List

20. Lambda Function with map


Summary of Week 4 Topics

  1. Decorators - Function wrappers, logging, timing.

  2. Generators & Iterators - Lazy evaluation, custom iterators.

  3. Threading & Multiprocessing - Parallel processing.

  4. Design Patterns - Singleton, Factory, Observer.

  5. Python Tricks - Dictionary merging, swapping, lambda.


Last updated