# Python function argument not resetting value between calls

**URL:** <https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382>\
**Category:** Ignition\
**Tags:** scripting, ignition80\
**Created:** [November 6, 2020, 6:20am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382 "2020-11-06T06:20:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 6, 2020, 6:20am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/1 "2020-11-06T06:20:45Z")

</div>

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**

```auto
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

```

---

<div class="post-metadata">

**Author:** ![iscrac](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/iscrac/32/49757_2.png) [@iscrac](https://forum.inductiveautomation.com/u/iscrac)\
**Post date:** [November 6, 2020, 9:30am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/2 "2020-11-06T09:30:33Z")

</div>

```auto
def browseTags(path, filter={}, recursive=False):
	ret = []

```

maybe?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 6, 2020, 9:37am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/3 "2020-11-06T09:37:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iscrac](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/iscrac/32/49757_2.png) [@iscrac](https://forum.inductiveautomation.com/u/iscrac)\
**Post date:** [November 6, 2020, 9:39am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/4 "2020-11-06T09:39:59Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 6, 2020, 9:46am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/5 "2020-11-06T09:46:09Z")

</div>

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

```auto
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) 😟  
Thanks for the ideas!

---

<div class="post-metadata">

**Author:** ![iscrac](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/iscrac/32/49757_2.png) [@iscrac](https://forum.inductiveautomation.com/u/iscrac)\
**Post date:** [November 6, 2020, 9:47am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/6 "2020-11-06T09:47:45Z")

</div>

great!  
There’s always something that we don’t know yet))))  
🙃  
You’re wellcome! 😊

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 6, 2020, 10:47am UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/7 "2020-11-06T10:47:05Z")

</div>

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

The proper way then to fix it is:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 6, 2020, 3:48pm UTC](https://forum.inductiveautomation.com/t/python-function-argument-not-resetting-value-between-calls/40382/8 "2020-11-06T15:48:38Z")

</div>

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