如何在 python "recursive" 函数中重置全局变量?
How to reset a global variable in python "recursive" function?
def getValueForSpecificKey(self,capturedLog,key):
''' Get value from log for any specific key,caputredLog must be a string'''
global Found
global returnValue
if type(capturedLog)==type({}):
for eachKey in capturedLog:
if eachKey==key:
Found=True
print "Found Key Match :",eachKey
print capturedLog[key]
listAppendValue=capturedLog[key]
#returnValue=capturedLog[key]
returnValue.append(listAppendValue)
print "="*1000
print "The value for key in the captured Log is -> ",capturedLog[key]
print " Return value "*10,(returnValue)
return Found,returnValue
self.getValueForSpecificKey(capturedLog[eachKey],key)
elif type(capturedLog) is list:
for elements in capturedLog:
self.getValueForSpecificKey(elements,key)
print returnValue
return Found,returnValue
我有这个函数递归迭代一个有效的 json.The json 可能包含 dict 或 list.The 函数在这里搜索需要一些输入日志,它是 json 和一个在 json.I 中搜索的密钥能够迭代以获取 json 中的所有密钥。一切都很好。
但是当我尝试 return value.Since returnValue 是一个全局变量时,问题就来了。追加到列表中,继续将所有值追加到相同的list.Even 当我从其他模块创建 class 的对象并调用此函数时,列表仍在增长,我无法 clear/reset returnValue before/after 函数 call.I 需要更改 returnValue ,它是每次从外部模块创建对象时的列表,即当第一次这个函数是 called.So 时,这是一个问题我需要用递归 return 值。
由于api已经定义了我只能更改其中的代码。
请帮忙。
谢谢
def getValueForSpecificKey(self,capturedLog,key,first=[])
global returnaValue
if not first:
first.append(1)
returnaValue = []
这将在您第一次调用时创建一个全局空列表。这将指示您应该清除全局列表 returnValue。无 api 更改并使用递归
我认为您应该将递归函数更改为不需要任何全局变量。如果你只希望找到一个结果,我认为它并不太复杂。
def getValueForSpecificKey(self, capturedLog, key):
'''
Get value from log for any specific key.
capturedLog will be searched if it is a list or dictionary.
Returns None if the key is not found, or capturedLog is another type.
'''
if isinstance(capturedLog, dict):
if key in capturedLog: # base case #1 (found the key)
return capturedLog[key]
values = capturedLog.values()
elif isinstance(capturedLog, list):
values = capturedLog
else:
return None # base case #2 (not a list or dict)
for value in values:
result = self.getValueForSpecificKey(value, key) # recursive case
if result is not None:
return result
return None # nothing found in the recursive search
如果您可能在不同的字典中有多个具有所需键的值,您可能需要传递一个列表:
def getValueForSpecificKey(self, capturedLog, key, results=None):
'''
Get value from log for any specific key.
capturedLog will be searched if it is a list or dictionary.
Returns an empty list if the key is not found, or capturedLog is another type.
'''
if results is None:
results = [] # start a new list if this is a non-recursive call
if isinstance(capturedLog, dict):
if key in capturedLog:
results.append(capturedLog[key]) # found a result, add it to the list
return results
values = capturedLog.values()
elif isinstance(capturedLog, list):
values = capturedLog
else:
return results
for value in values: # recursive calls will add to the results list as necessary
self.getValueForSpecificKey(value, key, results)
return results
好的,这是解决方案
在执行函数定义时评估默认参数值。这意味着在定义函数时表达式被计算一次,并且每次调用都使用相同的“预计算”值。当默认参数是可变对象(例如列表或字典)时,理解这一点尤为重要:如果函数修改了对象(例如,通过将项目附加到列表),则默认值实际上被修改了。这通常不是预期的。解决这个问题的一种方法是使用 None 作为默认值,并在函数体中显式测试它,例如:
def getValueForSpecificKey(self,capturedLog,key,First=None):
''' Get value from requests log for any specific key,caputredLog must be a string'''
global Found
global returnValue
if First is None:
First='notnone'
Found=False
returnValue=[]
print "Return Value ->",returnValue
print "Found - >",Found
if type(capturedLog)==type({}):
for eachKey in capturedLog:
print eachKey
if eachKey==key:
Found=True
print "Found Key Match :",eachKey
print capturedLog[key]
listAppendValue=capturedLog[key]
returnValue.append(listAppendValue)
print "="*1000
print "The value for key in the captured Log is -> ",capturedLog[key]
print " Return value "*10,(returnValue)
return Found,returnValue
print "CapturedLog - >",capturedLog
print "Calling using recursion "
self.getValueForSpecificKey(capturedLog[eachKey],key,First)
elif type(capturedLog) is list:
for elements in capturedLog:
self.getValueForSpecificKey(elements,key,First)
return Found,returnValue
def getValueForSpecificKey(self,capturedLog,key):
''' Get value from log for any specific key,caputredLog must be a string'''
global Found
global returnValue
if type(capturedLog)==type({}):
for eachKey in capturedLog:
if eachKey==key:
Found=True
print "Found Key Match :",eachKey
print capturedLog[key]
listAppendValue=capturedLog[key]
#returnValue=capturedLog[key]
returnValue.append(listAppendValue)
print "="*1000
print "The value for key in the captured Log is -> ",capturedLog[key]
print " Return value "*10,(returnValue)
return Found,returnValue
self.getValueForSpecificKey(capturedLog[eachKey],key)
elif type(capturedLog) is list:
for elements in capturedLog:
self.getValueForSpecificKey(elements,key)
print returnValue
return Found,returnValue
我有这个函数递归迭代一个有效的 json.The json 可能包含 dict 或 list.The 函数在这里搜索需要一些输入日志,它是 json 和一个在 json.I 中搜索的密钥能够迭代以获取 json 中的所有密钥。一切都很好。
但是当我尝试 return value.Since returnValue 是一个全局变量时,问题就来了。追加到列表中,继续将所有值追加到相同的list.Even 当我从其他模块创建 class 的对象并调用此函数时,列表仍在增长,我无法 clear/reset returnValue before/after 函数 call.I 需要更改 returnValue ,它是每次从外部模块创建对象时的列表,即当第一次这个函数是 called.So 时,这是一个问题我需要用递归 return 值。
由于api已经定义了我只能更改其中的代码。
请帮忙。 谢谢
def getValueForSpecificKey(self,capturedLog,key,first=[])
global returnaValue
if not first:
first.append(1)
returnaValue = []
这将在您第一次调用时创建一个全局空列表。这将指示您应该清除全局列表 returnValue。无 api 更改并使用递归
我认为您应该将递归函数更改为不需要任何全局变量。如果你只希望找到一个结果,我认为它并不太复杂。
def getValueForSpecificKey(self, capturedLog, key):
'''
Get value from log for any specific key.
capturedLog will be searched if it is a list or dictionary.
Returns None if the key is not found, or capturedLog is another type.
'''
if isinstance(capturedLog, dict):
if key in capturedLog: # base case #1 (found the key)
return capturedLog[key]
values = capturedLog.values()
elif isinstance(capturedLog, list):
values = capturedLog
else:
return None # base case #2 (not a list or dict)
for value in values:
result = self.getValueForSpecificKey(value, key) # recursive case
if result is not None:
return result
return None # nothing found in the recursive search
如果您可能在不同的字典中有多个具有所需键的值,您可能需要传递一个列表:
def getValueForSpecificKey(self, capturedLog, key, results=None):
'''
Get value from log for any specific key.
capturedLog will be searched if it is a list or dictionary.
Returns an empty list if the key is not found, or capturedLog is another type.
'''
if results is None:
results = [] # start a new list if this is a non-recursive call
if isinstance(capturedLog, dict):
if key in capturedLog:
results.append(capturedLog[key]) # found a result, add it to the list
return results
values = capturedLog.values()
elif isinstance(capturedLog, list):
values = capturedLog
else:
return results
for value in values: # recursive calls will add to the results list as necessary
self.getValueForSpecificKey(value, key, results)
return results
好的,这是解决方案
在执行函数定义时评估默认参数值。这意味着在定义函数时表达式被计算一次,并且每次调用都使用相同的“预计算”值。当默认参数是可变对象(例如列表或字典)时,理解这一点尤为重要:如果函数修改了对象(例如,通过将项目附加到列表),则默认值实际上被修改了。这通常不是预期的。解决这个问题的一种方法是使用 None 作为默认值,并在函数体中显式测试它,例如:
def getValueForSpecificKey(self,capturedLog,key,First=None):
''' Get value from requests log for any specific key,caputredLog must be a string'''
global Found
global returnValue
if First is None:
First='notnone'
Found=False
returnValue=[]
print "Return Value ->",returnValue
print "Found - >",Found
if type(capturedLog)==type({}):
for eachKey in capturedLog:
print eachKey
if eachKey==key:
Found=True
print "Found Key Match :",eachKey
print capturedLog[key]
listAppendValue=capturedLog[key]
returnValue.append(listAppendValue)
print "="*1000
print "The value for key in the captured Log is -> ",capturedLog[key]
print " Return value "*10,(returnValue)
return Found,returnValue
print "CapturedLog - >",capturedLog
print "Calling using recursion "
self.getValueForSpecificKey(capturedLog[eachKey],key,First)
elif type(capturedLog) is list:
for elements in capturedLog:
self.getValueForSpecificKey(elements,key,First)
return Found,returnValue