def timer(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f"Execution time: {time.time() - start:.2f}s")
return result
return wrapper
@timer
def slow_function():
import time
time.sleep(1)
return "Done"
Generators
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Generator expression
squares = (x**2 for x in range(10))
# Using generators
fib = fibonacci()
for _ in range(10):
print(next(fib))