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:
- originally posted in StackOverflow: (CC BY-SA 3.0)
- later modified by
Project details
Release history Release notifications | RSS feed
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)