Skip to main content

inject-globals

A function for injecting global variables into function calls.

Usage example:

A simple usage example is this:

from inject_globals import inject_globals

@inject_globals(a = "Hello", b = "World")
def hello():
    print(f"{a} {b}")

hello()

However, that example is somewhat silly. It's power shows up when you are creating other decorators, like this one:

def run_n_times(n):
    def middle(func):
        from functools import wraps

        @wraps(func)
        def inner(*args, **kwargs):
            r = []
            for i in range(1, n + 1):
                r.append(inject_globals(it = i)(func)(*args, **kwargs))
            return r
        return inner
    return middle

@run_n_times(3)
def foo():
    return f"This is the iteration #{it}."

assert foo() == ["This is the iteration #1.", "This is the iteration #2.", "This is the iteration #3."]

Release files for inject-globals 1.0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for inject-globals 1.0.0.4
File Size Uploaded
inject-globals-1.0.0.4.tar.gz 2.0 kB Details

Release files / inject-globals-1.0.0.4.tar.gz

Download URL inject-globals-1.0.0.4.tar.gz
Size 2.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d0864849061b21401e883053391ed4487f1407128965faa86410809f4ff9cda2
BLAKE2b-256 checksum
How to use checksums
6de805eae55a166d3f95c1ecf8d1056d9bfbcbfc8ca36cb977810f2f4289ee1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

1.0.0.4 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page