This presentation by Charles-Axel Dein on advanced Python features aims to improve code readability and showcase underused features. It covers concepts such as decorators, context managers, iterators, and special methods, demonstrating how they can enhance code expressiveness and efficiency. Additionally, it highlights useful Python modules and offers insights into profiling Python code for performance optimization.
An introduction to an advanced Python presentation by Charles-Axel Dein, licensed under CC BY-SA. GitHub repository shared for additional resources.
The presentation aims to discover underused Python features and improve code readability, emphasizing the importance of clear and concise coding practices.
Discusses how readability can be enhanced by conciseness and avoid 'magic' patterns, with code examples illustrating the evolution of a function's clarity.
Introduction to decorators in Python, describing them as syntactic sugar for attaching additional responsibilities to functions, along with practical use cases.
Explanation of context managers and their use in managing resources safely, including examples like file operations and Redis pipelines.
Overview of iterators and generators in Python, detailing their protocol, use cases for memory efficiency, and the benefits of lazy evaluation.
Description of special methods (dunder methods) for operator overloading, their significance in encapsulating logic, and practical applications.
Highlights several Python modules including collections, random, functools, and operator, showcasing their functionality with examples.
Simple methods for profiling Python code performance using cProfile, illustrating how to monitor function calls and timings.
Wrap-up of presentation, mentioning additional topics such as memory allocation, metaclasses, and other Python modules.
Encouragement to check the GitHub for additional resources and thanking the audience for their participation.
License
• This presentationis shared under Creative Commons
Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
• More information about the license can be found here
• Please give proper attribution to the original author
(Charles-Axel Dein)
3.
Checkout my Githubfor
the source of this presentation
and more resources:
github.com/charlax/python-education
In Python ,almost
everything is an object
In [1]: def toast(): pass
In [2]: toast
Out[2]: <function __main__.toast>
In [3]: type(toast)
Out[3]: function
Iterators: what arethey?
• An object that implements the iterator protocol
• __iter__() returns the iterator (usually self)
• __next__() returns the next item or raises StopIteration
• This method will be called for each iteration until it raises
StopIteration
Use cases ofiterators
• Lazy evaluation of results one (batch) at time
• Lower memory footprint
• Compute just what you needed - can break in the middle (e.g.
transparently page queries)
• Unbounded sets of results
46.
To go further
•Other uses of yield (e.g. coroutines)
• Exception handling
Why should theybe used?
• Greater encapsulation of the logic,
allowing objects to be manipulated without external function/methods
• Allow uses of builtins that all Python developers know (len, repr, …)
• Allow objects to be manipulated via operators (low cognitive burden)
• Allow use of certain keywords and Python features
• with (__enter__, __exit__)
• in (__contains__)
Other topics
• Memoryallocation trace
• Metaclasses
• Descriptors and properties
• List, dict, set comprehensions
• Other modules: itertools, csv, argparse, operator, etc.