在python3中引用全局变量时,处理`NameError`而不引发`SyntaxError`

时间:2017-09-01 19:32:45

标签: python python-3.x exception exception-handling decorator

处理NameError正在提升SyntaxError

所以我正在制作一个装饰器函数,它将生成器函数与字典相关联,目的是使send()函数不那么麻烦。

默认行为是使用在global(名称空间)中定义的名为global_registry_dict的字典。

如果global_registry_dict不存在且没有显式传入替代字典,则python会引发NameError

当我尝试通过在全局命名空间中创建变量来处理错误时,我得到一个SyntaxError告诉我在赋值之前引用了该变量。这是有道理的......但......就像......这就是我想要解决的问题!

非常感谢任何帮助!

装饰者的来源:

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])

1 个答案:

答案 0 :(得分:1)

您可以在global_registry_dict子句中使用名称else。如果它是全局的,那么在使用它之前必须声明它,就像错误消息所说的那样。通常,名称在函数开头声明为全局。