Hey guys!! We have a gift for you!!
noglobal
is the decorator that makes your Python code more strictly one with the global variables. No global variables will be worked in the function wrapped by noglobal
decorator unless set specifically explicit arguments as 'excepts'.
Note that you could not use the global variables that is not able to call, not module, or not specified as 'excepts'.
No PyPI, so you can't use command as pip
. If you wanna use noglobal
, please copy the code or clone this repository;
git clone https://github.com/K-PTL/noglobal-python
from noglobal import noglobal
a = "hoge"
def runner(f):
try:
f()
except NameError as ne:
print(ne)
print() # blank row
@noglobal()
def func_usual():
print("This is func_usual")
print(a) # raised NameError: name 'a' is not defined.
import numpy as np
@noglobal()
def func_nest():
print("This is func_nest")
print(np.arange(0,10)) # you can use module as numpy
print(a) # raised NameError: name 'a' is not defined.
run(func_usual()) # you can call it, but raised NameError inside the func_usual.
@noglobal(excepts=["a"])
def func_use_excepts():
print("This is func_use_excepts")
print(np.arange(0,10))
print(a) # NO NameError raised since specified 'a' as 'excepts'.
run(func_nest()) # Raised NameError because func_nest does not allow to
# use global variable 'a' while allowed to use in func_use_excepts.
>>> runner(func_usual)
This is func_usual
name 'a' is not defined
>>> runner(func_nest)
This is func_nest
[0 1 2 3 4 5 6 7 8 9]
name 'a' is not defined
>>> runner(func_use_excepts)
This is func_use_excepts
[0 1 2 3 4 5 6 7 8 9]
hoge
This is func_nest
[0 1 2 3 4 5 6 7 8 9]
name 'a' is not defined
I introduce you the previous noglobal
as below;
- noglobal::ver01 by ax3l
- noglobal::ver02 by raven38
- noglobal::ver03 by yoshipon
- noglobal::ver03-2 by K-PTL
- noglobal::ver04 by momijiame
My noglobal
function was builded on what has been laid down by previous contributers. Thanks so much.