简体   繁体   English

如何在函数外部访问函数内部产生的字典,而又不需多次调用函数。 蟒蛇

[英]How to access a dictionary produced within a function, outside the function, without calling the function more than once. PYTHON

I have a function that returns a dictionary. 我有一个返回字典的函数。

I would like to be able to access and work with that dictionary many times in the code, without the need to be calling the function that produces that dictionary every time. 我希望能够在代码中多次访问并使用该词典,而无需每次都调用生成该词典的函数。 In other words, call the function once, but work with the dictionary that it returns many times. 换句话说,一次调用该函数,但是使用它返回多次的字典。

So this way, the dictionary is constructed only once (and maybe stored somewhere?), but called and utilised many times in the script. 因此,以这种方式,字典仅构建一次(也许存储在某个地方?),但是在脚本中被调用和使用了很多次。

def function_to_produce_dict():
    dict = {}
    # something
    # something that builds the dictionary 
    return dict 


create_dict = function_to_product_dict()

# other code that will need to work with the create_dict dictionary. 
# without the need to keep constructing it any time that we need it. 

I have read other posts such as: Access a function variable outside the function without using `global` 我读过其他文章,例如: 不使用`global`即可访问函数外部的函数变量

But I am not sure that by declaring the dictionary as global withing the function_to_produce_dict() is going to make the dictionary accessible without having to build it every time, by calling the function again and again. 但是我不确定通过使用function_to_produce_dict()将字典声明为全局字典,可以通过一次又一次地调用函数来使字典可访问而不必每次都构建它。

Is this possible? 这可能吗?

Maybe I'm not understanding, but you're already saving it in create_dict ; 也许我不太了解,但是您已经将其保存在create_dict just continue to use the dict in create_dict . 只需继续使用create_dict的dict即可。 It's not being constructed each time once you store it in create_dict . 一旦将其存储在create_dict就不会每次都在构造它。

Perhaps you should rename it, as create_dict sounds like a function that creates a dict. 也许您应该重命名它,因为create_dict听起来像一个创建dict的函数。 Maybe that's related to your confusion on this? 也许这与您对此感到困惑有关?

It's a little unclear what is stopping you from just using the dictionary as usual. 尚不清楚是什么阻止您像往常一样仅使用字典。 Do you have a situation such as the following? 您是否遇到以下情况?

def function_to_produce_dict():
    ...

def foo():
    dct = function_to_produce_dict()
    # do something to dct
    ...

def bar():
    dct = function_to_produce_dict()
    # do something to dct
    ...

foo()
bar()

In such a case, you might want foo and bar to take an argument that is an already-constructed dct : 在这种情况下,您可能希望foobar接受一个已经构造dct

def foo(dct):
    # do something with dct

If you really can't get around it, you can cache the result of creating the dictionary so that it's really only computed once: 如果您真的无法解决它,则可以缓存创建字典的结果,以便实际上仅计算一次:

def _cached_function_to_compute_dict():
    dct = None
    def wrapper():
        if dct is None:
            dct = _function_to_compute_dict()
        return dct
    return wrapper

def _function_to_compute_dict():
    # create and return a dictionary
    return dct

function_to_compute_dict = _cached_function_to_compute_dict()

This is just a specialized memoization decorator... you could get a lot fancier, use functools.partial to preserve function metadata, etc. 这只是一个专门的备注装饰器...您可能会觉得更有趣,可以使用functools.partial来保存函数元数据,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM