Similarly, (using yield instead of return ) revolutionize memory management. Instead of loading a 10GB log file into RAM, a generator yields one line at a time, reducing memory footprint from gigabytes to kilobytes. Advanced patterns like generator pipelines (coroutines) allow data to flow through processing stages, mirroring Unix pipes within Python. 2. Object-Oriented Depth: Protocols and Composition While beginners learn classes and inheritance, advanced practitioners favor composition over inheritance . Python’s protocols (informal interfaces) enable "duck typing" without rigid hierarchies. For instance, implementing __len__ and __getitem__ makes a custom class behave like a sequence, compatible with len() and slicing.
Exception handling should be explicit and granular . Catching bare except: hides KeyboardInterrupt and system exits. Advanced code defines custom exception hierarchies and uses else (run if no exception) and finally (cleanup) purposefully. Python Nâng Cao is not a destination but a journey. It replaces "it works on my machine" with reproducible, documented, and testable engineering. The tools described—decorators, generators, async/await, type hints, and context managers—share a common goal: reducing cognitive load while increasing reliability. As the Python ecosystem evolves (e.g., pattern matching in 3.10, Self type in 3.11), the advanced practitioner remains a perpetual learner. True mastery is not knowing every feature, but knowing which feature simplifies a problem and why . Note to the reader: If this essay were the preface to a PDF titled Python Nâng Cao , the following chapters would include practical exercises on building a decorator-based retry mechanism, implementing an async web scraper, and refactoring a legacy script using type hints and dataclasses. The PDF would also contain Vietnamese-language explanations of key terminology (e.g., "trình trang trí" for decorators) to bridge conceptual gaps for local learners. python nang cao pdf
A common pitfall is using threads for CPU work—resulting in no speedup. Advanced developers know the "rule of thumb": I/O-bound → asyncio or threading; CPU-bound → multiprocessing. Libraries like concurrent.futures abstract this complexity, but understanding the underlying execution model is crucial. Python is dynamically typed, but large codebases benefit from type hints (PEP 484) and static checkers like mypy . Advanced typing includes TypeVar for generics, Protocol for structural subtyping, and Final for constants. This does not make Python statically typed, but it catches entire classes of bugs (e.g., passing a str to a parameter expecting int ) before runtime. Similarly, (using yield instead of return ) revolutionize