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 details)

Uploaded Source

File details

Details for the file inject-globals-1.0.0.4.tar.gz.

File metadata

  • Download URL: inject-globals-1.0.0.4.tar.gz
  • Upload date:
  • Size: 2.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for inject-globals-1.0.0.4.tar.gz
Algorithm Hash digest
SHA256 d0864849061b21401e883053391ed4487f1407128965faa86410809f4ff9cda2
MD5 c4a875fd44827dd814bef5b827867e0a
BLAKE2b-256 6de805eae55a166d3f95c1ecf8d1056d9bfbcbfc8ca36cb977810f2f4289ee1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page