Learn Advanced Python: Iterators, Generators, Decorators, Multithreading, and More - Textnotes

Learn Advanced Python: Iterators, Generators, Decorators, Multithreading, and More


Master advanced Python concepts including iterators, generators, decorators, context managers, regular expressions, multithreading, and logging for large-scale, efficient programs.

Objective:

Master Python to write efficient, maintainable, and scalable code for complex projects using advanced features and tools.

Topics and Examples:

1. Iterators & Generators: iter(), next(), yield

Iterators allow traversing through elements; generators create iterators dynamically using yield.

Example (Iterator):


my_list = [1, 2, 3]
it = iter(my_list)
print(next(it)) # 1
print(next(it)) # 2

Example (Generator):


def square_numbers(n):
for i in range(n):
yield i ** 2

gen = square_numbers(5)
for num in gen:
print(num)

2. Decorators: Function Decorators, Class Decorators

Decorators modify the behavior of functions or classes without changing their code.

Function Decorator Example:


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

@decorator
def greet():
print("Hello, Chinmaya!")

greet()

3. Context Managers: with Statement

Context managers manage resources (like files) efficiently, ensuring cleanup after use.

Example:


with open("example.txt", "w") as f:
f.write("Hello World!")
# File is automatically closed

Custom Context Manager:


class MyContext:
def __enter__(self):
print("Enter context")
return self

def __exit__(self, exc_type, exc_val, exc_tb):
print("Exit context")

with MyContext():
print("Inside context")

4. Regular Expressions: re Module

Regular expressions are used for pattern matching in strings.

Example:


import re

text = "My email is example@mail.com"
pattern = r"\S+@\S+\.\S+"

match = re.search(pattern, text)
if match:
print("Email found:", match.group())

5. Multithreading & Multiprocessing

  1. Multithreading: Run multiple threads concurrently (for I/O-bound tasks)
  2. Multiprocessing: Run processes in parallel (for CPU-bound tasks)

Multithreading Example:


import threading

def print_numbers():
for i in range(5):
print(i)

thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()

Multiprocessing Example:


from multiprocessing import Process

def print_numbers():
for i in range(5):
print(i)

process = Process(target=print_numbers)
process.start()
process.join()

6. Logging and Debugging

Logging helps track program execution; debugging helps find and fix issues.

Logging Example:


import logging

logging.basicConfig(level=logging.INFO)
logging.info("This is an info message")
logging.error("This is an error message")

Debugging Example:


x = 10
y = 0
try:
z = x / y
except ZeroDivisionError as e:
print("Error occurred:", e)

This section covers all essential advanced Python concepts, including iterators, generators, decorators, context managers, regular expressions, multithreading, multiprocessing, logging, and debugging, with practical examples for real-world projects.