如何在 Python 中的函数调用之间维护列表和字典? [英] How to maintain lists and dictionaries between function calls in Python?
问题描述
我有一个功能.在里面,我正在维护一个值字典.我希望在不同的函数调用之间维护该字典
I have a function. Inside that I'm maintainfing a dictionary of values. I want that dictionary to be maintained between different function calls
假设 dic 是:
a = {'a':1,'b':2,'c':3}
在第一次调用时,比如说,我将 a[a] 改为 100字典变成 a = {'a':100,'b':2,'c':3}
At first call,say,I changed a[a] to 100
Dict becomes a = {'a':100,'b':2,'c':3}
在另一个电话中,我将 a[b] 改为 200我希望那个 dic 是 a = {'a':100,'b':200,'c':3}
At another call,i changed a[b] to 200
I want that dic to be a = {'a':100,'b':200,'c':3}
但在我的代码中,a[a] 不保持为 100.它更改为初始值 1.
But in my code a[a] doesn't remain 100.It changes to initial value 1.
我需要尽快答复....我已经迟到了...请朋友们帮帮我...
I need an answer ASAP....I m already late...Please help me friends...
推荐答案
您可能在谈论可调用对象.
You might be talking about a callable object.
class MyFunction( object ):
def __init__( self ):
self.rememberThis= dict()
def __call__( self, arg1, arg2 ):
# do something
rememberThis['a'] = arg1
return someValue
myFunction= MyFunction()
从那时起,将 myFunction 用作一个简单的函数.您可以使用 myFunction.rememberThis
访问 rememberThis
字典.
From then on, use myFunction as a simple function. You can access the rememberThis
dictionary using myFunction.rememberThis
.
这篇关于如何在 Python 中的函数调用之间维护列表和字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!