处理NameError就是引发SyntaxError
因此,我制作了一个装饰器函数,它将生成器函数与字典相关联,目的是使send()函数使用起来不那么麻烦。
默认行为是使用中定义的名为global_registry_dict的字典(令人惊讶!)全局命名空间。
如果global_registry_dict不存在,并且没有显式传入替代字典,则NameError将由python引发。
这是有道理的……但是……就像……这就是我要解决的问题!
任何帮助都将不胜感激!
the Decorator的来源:
def make_stateless_actor(func):
def wrapper(registry_dict=None, *args, **kwargs):
try:
if registry_dict is None:
internal_registry_dict = global_registry_dict
elif isinstance(registry_dict, dict) or issubclass(registry_dict, dict):
internal_registry_dict = registry_dict
else:
internal_registry_dict = global_registry_dict
except NameError: # <<== CATCHES IF `global_registry_dict` IS REF'D BUT NOT DEFINED.
global global_registry_dict # <<== SYNTAX ERROR HAPPENS HERE
global_registry_dict = {}
internal_registry_dict = global_registry_dict
gen = func(*args, **kwargs)
next(gen)
internal_registry_dict[func.__name__] = gen
return wrapper返回异常:
File "C:/yall/dont/need/to/know/this/generator_pipeline_utils.py", line 74
global global_registry_dict
^
SyntaxError: name 'global_registry_dict' is used prior to global declaration整个模块的源代码:
# global_registry_dict = {}
def make_stateless_actor(func):
"""
DESCRIPTION:
===========
This DECORATOR FUNCTION simplifies & centralizes the use of `send()` on generators.
It does this by ASSOCIATING the decorated function to a dictionary:
- by default, a global variable named `global_registry_dict` will be used.
- by default, `global_registry_dict = {}` will be created in the GLOBAL-NAMESPACE if it does not exist.
IMPORTANT NOTE:
==============
Use the `registry_dict` kwarg to manually assign your generator-func to an alternate dictionary:
`foo_decorated_func(registry_dict=foo_alternative_dictionary)`
REQUIREMENTS:
============
You can apply `@make_stateless_actor` to any GENERATOR FUNCTION which:
- INCLUDES A `yield` expression to the RIGHT of an `=` sign.
- INCLUDES `**kwargs` as a function argument.
"""
def wrapper(registry_dict=None, *args, **kwargs):
try:
if registry_dict is None: # <<-- necessary to prevent the next elif from throwing an exception.
internal_registry_dict = global_registry_dict
elif isinstance(registry_dict, dict) or issubclass(registry_dict, dict):
internal_registry_dict = registry_dict
else:
internal_registry_dict = global_registry_dict
except NameError:
print('WARNING: global_registry_dict IS NOT DEFINED AND YOU DID NOT EXPLICITLY PASS registry_dict TO THE DECORATED FUNCTION.\n\
`global_registry_dict = {}` has been instantiated in the global-namespace.\n')
global global_registry_dict
global_registry_dict = {}
internal_registry_dict = global_registry_dict
gen = func(*args, **kwargs)
next(gen)
internal_registry_dict[func.__name__] = gen
return wrapper
@make_stateless_actor
def printer(*args, **kwargs):
while True:
item = yield
print("Got: ", item)
def send(name, msg, registry_dict=None):
if registry_dict is None:
global_registry_dict[name].send(msg)
else:
registry_dict[name].send(msg)
if __name__ == "__main__":
printer(registry_dict=None) #<<==BEGIN THE CRAZY.
# send('printer', 'HEY WHAT IS UP?')
# send('printer', 666)
# send('printer', [1, 'a', [], True])发布于 2017-09-02 03:36:54
您在else子句中使用了名称global_registry_dict。如果它是全局的,就必须在使用它之前声明它,就像错误消息所说的那样。通常,名称在函数开始时声明为全局的。
https://stackoverflow.com/questions/46006878
复制相似问题