mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-10-09 14:31:13 +00:00
New Posts
This commit is contained in:
parent
a3ed2efc3b
commit
42fd45d2ba
4 changed files with 148 additions and 0 deletions
33
content/blog/exportpydecorator.md
Normal file
33
content/blog/exportpydecorator.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
---
|
||||
title: "Quick Python: Export Decorator"
|
||||
date: 2020-06-14T22:15:38-04:00
|
||||
draft: false
|
||||
tags: []
|
||||
---
|
||||
|
||||
A great [StackOverflow post](https://stackoverflow.com/a/35710527) by [Aaron Hall](https://stackoverflow.com/users/541136/aaron-hall) that shows how you can create an `export` decorator in order to not have to specify all the names you want to expose via [`__all__`](https://brandonrozek.com/blog/pythonall/).
|
||||
|
||||
The Decorator:
|
||||
|
||||
```python
|
||||
import sys
|
||||
|
||||
def export(fn):
|
||||
mod = sys.modules[fn.__module__]
|
||||
if hasattr(mod, '__all__'):
|
||||
mod.__all__.append(fn.__name__)
|
||||
else:
|
||||
mod.__all__ = [fn.__name__]
|
||||
return fn
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```python
|
||||
__all__ = []
|
||||
|
||||
@export # otherwise __all__ = ['test']
|
||||
def test():
|
||||
print("test")
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue