Python function argument not resetting value between calls

Maybe it’s just Friday afternoon brain… but I can’t figure this out.

I have a function that calls itself recursively to mimic the recursive behaviour of the old browseTags function (most of the script i stole from the forum with some changes), however the argument that is returned, ret, isn’t getting its value cleared each time the function is called… How can i ensure it’s cleared, without having to specify the default/cleared value when calling it. I want to just specify path, filter, and recursive

def browseTags(path, filter={}, recursive=False, ret=[]):
	# First, browse for anything that can have children (Folders and UDTs, generally)
	if str(path).find("[") == -1:
		path = '[default]' + str(path)
	if recursive:
		results = system.tag.browse(path)
		if results.getResults() is not None:
			for branch in results.getResults():    
				if branch['hasChildren']:
					# If something has a child node, then call this function again so we can search deeper.
					# Include the filter, so newer instances of this call will have the same filter.
					ret = browseTags(path=branch['fullPath'], filter=filter, recursive=recursive,ret=ret)
	
	# Call this function again at the current path, but apply the filter.
	results = system.tag.browse(path, filter)
	if results.getResults() is not None:
		for result in results.getResults():
			ret.append(result)
	return ret
def browseTags(path, filter={}, recursive=False):
	ret = []

maybe?

Can’t do that as it would then clear all results collected along the way when being recursively called

maybe You can collect the result… maybe this way (using for or something else):
result.append(browseTags(path,filter,recursive))
or something like that
and clear it after the whole script was done…

1 Like

Yep, that does it, well I used something similar:

def browseTags(path, filter={}, recursive=False, ret=[]):
	# First, browse for anything that can have children (Folders and UDTs, generally)
	if str(path).find("[") == -1:
		path = '[default]' + str(path)
	if recursive:
		results = system.tag.browse(path)
		if results.getResults() is not None:
			for branch in results.getResults():    
				if branch['hasChildren']:
					# If something has a child node, then call this function again so we can search deeper.
					# Include the filter, so newer instances of this call will have the same filter.
					ret = browseTags(path=branch['fullPath'], filter=filter, recursive=recursive,ret=ret)
	
	# Call this function again at the current path, but apply the filter.
	results = system.tag.browse(path, filter)
	if results.getResults() is not None:
		for result in results.getResults():
			ret.append(result)
	ret2 = ret
	ret = []
	return ret2

Very strange… Maybe I don’t understand Python as well as I thought (which wasn’t really all that well let’s be honest) :worried:
Thanks for the ideas!

1 Like

great!
There’s always something that we don’t know yet))))
:upside_down_face:
You’re wellcome! :blush:

This describes the “issue”:
http://effbot.org/zone/default-values.htm

The proper way then to fix it is:

def browseTags(path, filter={}, recursive=False, ret=None):
	if ret==None: ret=[]
	# First, browse for anything that can have children (Folders and UDTs, generally)
	if str(path).find("[") == -1:
		path = '[default]' + str(path)
	if recursive:
		results = system.tag.browse(path)
		if results.getResults() is not None:
			for branch in results.getResults():    
				if branch['hasChildren']:
					# If something has a child node, then call this function again so we can search deeper.
					# Include the filter, so newer instances of this call will have the same filter.
					ret = browseTags(path=branch['fullPath'], filter=filter, recursive=recursive,ret=ret)
	
	# Call this function again at the current path, but apply the filter.
	results = system.tag.browse(path, filter)
	if results.getResults() is not None:
		for result in results.getResults():
			ret.append(result)
	return ret
2 Likes

Dang, you caught it yourself, but yes, this is a known Python footgun due to the way functions are instantiated.