Skip to main content

A decorator for injecting global variables into function calls.

Project description

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."]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inject-globals-1.0.0.4.tar.gz (2.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page