创建一个装饰器/缓存来检查全局变量 [英] Creating a decorator / cache for checking global variable

查看:126
本文介绍了创建一个装饰器/缓存来检查全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多函数使用一些全局变量来保存整个库中的对象,例如:

I've quite a few functions that uses some global variables to hold an object to be reused throughout the library, e.g.:

from some.other.lib import Object1, Object2, Object3, Object4, Object5

def function1(input):
    global _object1
    try:
        _object1
    except NameError:
        _object1 = Object1
    return _object1.func1(input)


def function2(input):
    global _object2
    try:
        _object2
    except NameError:
        _object2 = Object2
    # Use function1 to do something to the input
    return _object2.func2(function1(input))


def function3(input):
    global _object3
    try:
        _object3
    except NameError:
        _object3 = Object3
    # Use function1 to do something to the input.
    return _object3.func3(function1(input))


def function4(input):
    global _object4
    try:
        _object4
    except NameError:
        _object4 = Object4
    return _object4.func4(input)


def function5(input):
    global _object5
    try:
        _object5
    except NameError:
        _object5 = Object5
    # Use function4 to do something to the input.
    return _object5.func5(function4(input))

有没有办法写一个装饰器,以便检查全局变量的代码不需要为每个函数手动硬编码*

Is there a way to write a decorator such that the code to check the global variable doesn't need to be manually hardcoded for each of the function*?

例如例如:

E.g. something like:

def exist_decorator(_object, _class):
    try:
        _object
    except NameError:
        _object = _class
    return _object

所以实际的类实现对于 function4()看起来像:

So the actual class implementation for function4() will look like:

def function4(input):
    global _object4
    exist_decorator(_object4, Object4)
    return _object4.func4(input)

类似于Python中使用装饰器在语法上的可能性吗?如果不是,我还可以创建装饰器/缓存来检查全局变量吗?

Is something like this syntactically possible in Python with decorators? If not, how else can I create a decorator/cache for checking global variable?

推荐答案

我想你只是想缓存一些值。为此,体面Python中的直接方法并未滥用任何隐藏功能,如下所示:

I guess you just want to cache some values. For this the straight forward approach in decent Python without abusing any hidden feature is this:

cache = {}

def function1(input):
    try:
        object1 = cache['object1']
    except KeyError:
        object1 = cache['object1'] = Object1
    return object1.func1(input)

与其他函数类似。

通过在函数对象中存储所有内容,您还可以避免使用静态全局变量 cache >

You can also avoid using the still-global variable cache by storing everything within your function object:

def function1(input):
    try:
        object1 = function1.object1
    except AttributeError:
        object1 = function1.object1 = Object1
    return object1.func1(input)

这是可能的,因为函数是可以添加属性的任意对象。但有些人可能把这称为滥用和不洁的代码。像往常一样,在这种情况下,首先与你的团队讨论这个问题,也许在使用它之前将它添加到这个团队或项目使用的技术列表中。

This is possible because functions are arbitrary objects to which you can add attributes. But some might call this an abuse and unclean code. As always in such cases, discuss this with your team first and maybe add it to the list of used techniques for this team or project before using it.

我更喜欢使用

I prefer using the hidden feature of mutable arguments:

def function(input, object1Cache=[None]):
    if object1Cache[0] is None:
        object1Cache[0] = Object1
    return object1Cache[0].func1(input)

这适用于Python,因为列表作为函数参数的默认值仍然是可变的,并且会保留其值。

This works in Python because a list as a default value for a function parameter is still mutable and will keep its value.

同样,这可能会被认为是不洁净和滥用,所以在使用它之前与你的团队讨论这个问题,并在你的项目中记录使用这种技术。

And again, this might be considered unclean and an abuse, so discuss this with your team before using it and document in your project notes that this technique is used.

这篇关于创建一个装饰器/缓存来检查全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆