https://github.com/pomponchik/contextif
Run the function only in a specific context
https://github.com/pomponchik/contextif
Last synced: 11 months ago
JSON representation
Run the function only in a specific context
- Host: GitHub
- URL: https://github.com/pomponchik/contextif
- Owner: pomponchik
- License: mit
- Created: 2023-11-11T12:57:49.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-07T20:29:24.000Z (over 2 years ago)
- Last Synced: 2025-06-09T21:07:17.946Z (about 1 year ago)
- Language: Python
- Size: 93.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://pepy.tech/project/contextif)
[](https://pepy.tech/project/contextif)
[](https://codecov.io/gh/pomponchik/contextif)
[](https://hitsofcode.com/github/pomponchik/contextif/view?branch=main)
[](https://github.com/pomponchik/contextif/actions/workflows/tests_and_coverage.yml)
[](https://pypi.python.org/pypi/contextif)
[](https://badge.fury.io/py/contextif)
[](http://mypy-lang.org/)
[](https://github.com/astral-sh/ruff)
# contextif: run the function only in a specific context
Sometimes we may need to run a function only if it happens in a strictly defined context. In this case, we can use this mini library.
Install it:
```bash
pip install contextif
```
And use:
```python
from contextif import state
with state:
state(print, 'hello,', 'world!') # It will be printed.
state(print, "it's me, Mario!") # It won't.
```
Using `state` as a function, you can pass another function and arguments to it there. It will be called only if it happens in a context created also using `state`. The function will not be called out of context.
For convenience, after the first import of `state`, this variable becomes builtin and you can access it in other modules of your program without importing.