Utility for initialization that runs an extra function once before a function is called
Project description
English | 简体中文
init_once
Introduction
init_once is a decorator that ensures an initialization function is executed only once before the first call of the decorated function. The initialization logic will not be repeated on subsequent calls to the decorated function.
Dependencies and Version Requirements
- Python 3.8 or higher
Usage
Basic Syntax
@init_once(initializer, *init_args, **init_kwds)
def func(...):
...
initializer: The initialization function that needs to be executed once.*init_args: Positional arguments to pass toinitializer.**init_kwds: Keyword arguments to pass toinitializer.
Simple Example
from init_once import init_once
# Initialization function: runs only once
def init_func(msg: str):
print(f"Initializing msg: {msg}")
@init_once(init_func, "hello")
def main_task(data: str):
print(f"Processing: {data}")
# First call: init_func executes first, then main_task
main_task("first")
# Output:
# Initializing msg: hello
# Processing: first
# Subsequent calls: only main_task executes
main_task("second")
# Output:
# Processing: second
Notes
- Applicability: This decorator is only suitable for simple initialization procedures. Its return value is ignored. If you need to obtain the return value, it indicates that this decorator should not be used for implicit initialization.
- Async and Threading: Async and thread-safe versions are to be implemented in the future.
License
This project is licensed under the MIT License. See the LICENSE file for details.
If you have any questions or suggestions for improvement, feel free to open an Issue or submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file init_once-0.1.0.tar.gz.
File metadata
- Download URL: init_once-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f0d212c22fedcf090c5fdb484a5464cbfec1a343ae1de9f3036f8cc25988241
|
|
| MD5 |
18a43ab1ac55a5b03d5d6cfc32a5fcb4
|
|
| BLAKE2b-256 |
c918c36b23ae57599f68f7dc93674600bd3eb5df35558066e58abaac81be95c9
|
File details
Details for the file init_once-0.1.0-py3-none-any.whl.
File metadata
- Download URL: init_once-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f20ddc26cb33068b82eb08570c78c9010f40d16e73e6258d16641ff8089193d
|
|
| MD5 |
c3a6ed90eef849c8afc905daf3664f8b
|
|
| BLAKE2b-256 |
d37edee828c6c2cfcd7116456c8fb8b88ad5fe39d225e084a1e80e0c968e960a
|