Skip to main content

My python utility functions

Project description

noglobal

This python package provides the @noglobal decorator which prohibits the use of global variables from the function scope.

Install

pip install noglobal

Usage

from noglobal import NoGlobal
noglobal = NoGlobal(globals()).noglobal

bar = 10
@noglobal
def foo():
    print(bar)
foo()
NameError                                 Traceback (most recent call last)
Input In [4], in <cell line: 5>()
      2 @noglobal
      3 def foo():
      4     print(bar)
----> 5 foo()

Input In [4], in foo()
      2 @noglobal
      3 def foo():
----> 4     print(bar)

NameError: name 'bar' is not defined

Note

Functions with the @noglobal decorator also disallow the explicit use of global variables. That is, if you update a variable bound by a global declaration within a function, the original global variable will not be affected. The following code prints 10 on the standard output.

bar = 10

@noglobal
def foo():
    global bar
    bar = 11
foo()
print(bar)

Attribution

Based on the source codes:

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

noglobal-1.0.2.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

noglobal-1.0.2-py3-none-any.whl (3.2 kB view hashes)

Uploaded Python 3

Supported by

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