Getting the Most Out of Python Imports
- Type:
- Talk
- Audience level:
- Intermediate
- Category:
- Core Python (Language, Stdlib)
March 10th 2:15 p.m. – 2:55 p.m.
Description
To really take advantage of Python you must understand how imports work and how to use them effectively. In this talk we'll discuss both of these. After a short introduction to imports, we'll dive right in and look at how customizing import behavior can make all your wildest dreams come true.
Abstract
Python's import statement has been a powerful feature since the first release, and only gotten better with age. Understanding how imports work under the hood will let you take advantage of that power.
A big key to customizing Python's imports is the importers introduced by PEP 302. That's a tool that you want in your belt! We'll be covering such import hooks as well as a couple other customization methods.
Talk Outline
- introduction and overview (2 min)
- under the hood (5 min)
- import syntax
- builtins.import
- PEP 302
- import state
- customizing Python (5 min)
- examples in the wild (5 min)
- 2 "walk-through" examples (5 min)
- trying out new Python syntax
- stat-averse imports
- 5 "flavor" examples (5 min)
- descriptors for module objects
- circumventing gotchas
- sneaking directives into import names
- managing multiple import hooks
- context manager for sys.path
- "lightning" examples (5 min)
- quick look at a bunch of working examples of cool stuff you can do with imports
- here's a sample:
- post-import hooks
- a python preprocessor
- naive macros and inline functions
- packages win noisily
- lazy imports
- conclusion
Supplemental Information
I have a lot more information at my Python Imports reference page.