Python's simplicity and power make it one of the most popular programming languages. This comprehensive guide will help you elevate your Python skills from intermediate to advanced level.
Advanced Python Concepts
1. Decorators and Metaprogramming
Function Decorators
import functools
import time
from typing import Callable, Any
def timer(func: Callable) -> Callable:
"""Decorator to measure function execution time."""
@functools.wraps(func)
def wrapper(*args, **kwargs) -> Any:
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"{func.__name__} took {end - start:.4f} seconds")
return result
return wrapper
@timer
def fibonacci(n: int) -> int:
"""Calculate nth Fibonacci number."""
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
Class Decorators
def singleton(cls):
"""Ensure only one instance of a class exists."""
instances = {}
@functools.wraps(cls)
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper
@singleton
class DatabaseConnection:
def __init__(self, host: str, port: int):
self.host = host
self.port = port
print(f"Connecting to {host}:{port}")
2. Context Managers
Custom Context Managers
from contextlib import contextmanager
import logging
@contextmanager
def timing_context(name: str):
"""Context manager for timing code blocks."""
start = time.time()
try:
yield
finally:
end = time.time()
logging.info(f"{name} completed in {end - start:.4f} seconds")
3. Generators and Iterators
Advanced Generator Patterns
def fibonacci_generator():
"""Generate Fibonacci numbers infinitely."""
a, b = 0, 1
while True:
yield a
a, b = b, a + b
def take(n: int, iterable):
"""Take first n items from an iterable."""
return list(itertools.islice(iterable, n))
4. Advanced Data Structures
Collections Module
from collections import namedtuple, defaultdict, Counter, deque
from typing import Dict, List
# Named tuples for immutable data
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
# DefaultDict for avoiding KeyError
word_counts: Dict[str, int] = defaultdict(int)
text = "the quick brown fox jumps over the lazy dog"
for word in text.split():
word_counts[word] += 1
# Counter for counting hashable objects
letter_counts = Counter(text.replace(' ', ''))
print(letter_counts.most_common(3))
5. Async Programming
Async/Await Patterns
import asyncio
import aiohttp
from typing import List, Dict
async def fetch_url(session: aiohttp.ClientSession, url: str) -> Dict:
"""Fetch a single URL asynchronously."""
try:
async with session.get(url) as response:
return {
'url': url,
'status': response.status,
'content_length': len(await response.text())
}
except Exception as e:
return {'url': url, 'error': str(e)}
async def fetch_multiple_urls(urls: List[str]) -> List[Dict]:
"""Fetch multiple URLs concurrently."""
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
results = await asyncio.gather(*tasks)
return results
Best Practices Summary
1. Code Organization
- Use modules and packages effectively
- Follow PEP 8 style guidelines
- Implement proper documentation with docstrings
- Use type hints for better code clarity
2. Performance
- Profile before optimizing
- Use appropriate data structures
- Leverage generators for memory efficiency
- Consider caching for expensive operations
3. Error Handling
- Use specific exception types
- Implement proper logging
- Handle errors gracefully
- Provide meaningful error messages
4. Testing
- Write unit tests for all functions
- Use pytest for testing framework
- Implement integration tests
- Consider property-based testing
5. Security
- Validate all inputs
- Use secrets module for sensitive data
- Implement proper authentication
- Follow security best practices
Conclusion
Mastering Python requires understanding both its advanced features and best practices. Focus on writing clean, efficient, and maintainable code.
Key takeaways:
- Use Python's built-in features effectively
- Write readable and maintainable code
- Understand performance implications
- Implement proper error handling
- Follow community best practices