r/Python • u/poppy_92 • Jul 31 '26
News PEP 842: Module Exports
https://peps.python.org/pep-0842/
Not the author, but this seems interesting, specially for library authors. Thoughts?
Discussion here - https://discuss.python.org/t/pep-842-module-exports/108353
24
Upvotes
View all comments
Show parent comment
3
u/HommeMusical Aug 01 '26
"Pythonic" doesn't mean "a thing that exists in Python" - it means adhering to the spirit of PEP 20, The Zen of Python.
Lazy imports solve several constant real-world problems with a single, perfectly clear word,
lazy.All previous code still works. Code using the variable
lazystill works. It's simple, it's explicit, it's readable, elegant, and practical. It's Pythonic in almost every way.But PEP 842 doesn't solve any real-world problem. "Someone might do this stupid thing, and I should prevent it."
We already have two ways to do this.
If you want to explicitly list what members you are exposing, use
__all__, it really isn't that hard.Or don't use
__all__, but name things starting with_, and if they use one of them, or they re-use one of your imports, well, maybe they have a good reason to.If you want to specify an API, you have type stubs.
If you just want to specify which symbols to export, you have
__all__, though that isn't enforced.Or mark variables with
_, though that isn't enforced either. Most of us like it that it's not enforced.Adding a fourth and more draconian way to do this, a little obscure, and just for imports, is a non-starter.
Adding an API with type stubs, that's really what you should be doing! Annoying fiddling with imports is picayune.