Skip to main content

Share global variables across modules

Project description

intermodule

This library makes it possible to share global variables across modules.

Api Reference

class intermodule.SharedGlobal

Instances of this class manage a global variable and keep it synchronized across modules. You should never instantiate this class directly. Instead, use set_global and get_global functions. This class inherits a variant of ObjectProxy from wrapt. Along with all augmented assignment operators, this class also overloads the normal assignment operator (=) thanks to assign-overload. Underlying global variable can be any object and the resulting instance of this class will act like that object in every way. If you use this class, you need to call intermodule.patch_and_reload_module(). You can also access this function from assign_overload module. You can find documentation about this function in assign-overload.

SharedGlobal.set_global(name, value)

Creates a new SharedGlobal instance with the given name and value but doesn't return it. Instead, assigns it to the calling module. Lastly, assigns it to every module which has a global variable whose name is name and whose value is a SharedGlobal instance.

SharedGlobal.get_global(name)

Search through every module for a already defined global variable named name. If found, check if it is a SharedGlobal instance. If it is, assign its value to the calling module. The value won't be returned. If no module defines a variable named name or if its value is not a SharedGlobal instance, raise an error.

Example

# module1.py
import intermodule

intermodule.SharedGlobal.set_global("x", 10)

def main():
    global x
    import module2
    module2.func1()
    print(x) # prints 20
    x = 30
    module2.func2()

if intermodule.patch_and_reload_module():
    main()
# module2.py
import intermodule

intermodule.SharedGlobal.get_global("x")

def func1():
    global x
    x = 20

def func2():
    print(x) # prints 30

if intermodule.patch_and_reload_module():
    pass

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

intermodule-1.0.0.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

intermodule-1.0.0-py2.py3-none-any.whl (3.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file intermodule-1.0.0.tar.gz.

File metadata

  • Download URL: intermodule-1.0.0.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for intermodule-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a38f3e7804761f6e96c4d339d318081b6864e5448ebd86c58a0a3155285726a6
MD5 5174fe63ea864a9fda5e3192a9892b88
BLAKE2b-256 a1abe5410c81599d998f63639065058f02d18fbf9800758e5542d51de0f7080d

See more details on using hashes here.

File details

Details for the file intermodule-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: intermodule-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for intermodule-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 09d425476d068a4297551b66b9712e9399d034cf6081d28eff86724bf96ea4af
MD5 a81a56ac289092d3172d447b219d1f22
BLAKE2b-256 9bbc3f8ba2fbb78ed3666a13efd6eb297a429ffa2ac4693c4ff7418b732111ad

See more details on using hashes here.

Supported by

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