如何使用python装饰器修复“ NoneType”错误?

时间:2019-06-30 11:28:38

标签: python python-3.6 python-decorators

我正在使用带有装饰器的LRU(最近最少使用)缓存来存储最近使用的本地化,但是当我调用函数从.json文件中读取它时,会出现“ NoneType”错误

def loc_cache(func):
    loc_cache.locale = {} # {lang:(local, count)} local:dict
    loc_cache.MAXLENGTH = 5
    def wrapper(key):
        print(key) #this function wasn't called
        if key not in loc_cache.locale.keys():
            try:
                locale = read_locale(key)
                loc_cache.locale[key] = (locale, 1)
                wrapper.locale = locale
            except KeyError:
                key = "en" # set default locale
                wrapper(key)
        else:
            locale, count = loc_cache.locale[key]
            loc_cache.locale[key] = (locale, count+1)
            wrapper.locale = locale
        return wrapper.locale

@loc_cache
def read_locale(key):
    locale = read_json("./config/locale.json", key)
    return locale

def auth(user:User):
    locale = read_locale(user.locale)
    print(locale["auth"])
    return

u = User(1) # __init__ takes 1 for id
u.locale = "en"
auth(u)

我希望它会返回存储在.json文件中的“ en”中的词组,但是它说

Traceback (most recent call last):
  File "main.py", line 61, in <module>
    auth(u)
  File "main.py", line 52, in auth
    locale = read_locale(user.locale)
TypeError: 'NoneType' object is not callable

2 个答案:

答案 0 :(得分:2)

您没有从装饰器返回包装函数,因此Python照常返回None并尝试在执行read_locale(user.locale)时调用它。您需要:

def loc_cache(func):
    loc_cache.locale = {} # {lang:(local, count)} local:dict
    loc_cache.MAXLENGTH = 5
    def wrapper(key):
        print(key) #this function wasn't called
        if key not in loc_cache.locale.keys():
            try:
                locale = read_locale(key)
                loc_cache.locale[key] = (locale, 1)
                wrapper.locale = locale
            except KeyError:
                key = "en" # set default locale
                wrapper(key)
        else:
            locale, count = loc_cache.locale[key]
            loc_cache.locale[key] = (locale, count+1)
            wrapper.locale = locale
        return wrapper.locale
    return wrapper
    # Here ^^^^

答案 1 :(得分:0)

您没有在装饰器的结尾退回您的wrapper

def loc_cache(func):
    loc_cache.locale = {} # {lang:(local, count)} local:dict
    loc_cache.MAXLENGTH = 5
    def wrapper(key):
        if key not in loc_cache.locale:
            try:
                locale = read_locale(key)
                loc_cache.locale[key] = (locale, 1)
            except KeyError:
                key = "en" # set default locale
                wrapper(key)
        else:
            locale, count = loc_cache.locale[key]
            loc_cache.locale[key] = (locale, count+1)
        wrapper.locale = locale
        return wrapper.locale
    return wrapper

装饰器通常将一个函数作为输入,然后返回一个函数。在这里,您定义了一个内部函数,但是您忘了返回它。结果,装饰器的输出为None,并且您无法调用None