# Using lambdas in Gateway event scripts

**URL:** <https://forum.inductiveautomation.com/t/using-lambdas-in-gateway-event-scripts/19548>\
**Category:** Ignition\
**Created:** [September 5, 2018, 4:13pm UTC](https://forum.inductiveautomation.com/t/using-lambdas-in-gateway-event-scripts/19548 "2018-09-05T16:13:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdecelles](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bdecelles/32/5713_2.png) [@bdecelles](https://forum.inductiveautomation.com/u/bdecelles)\
**Post date:** [September 5, 2018, 4:13pm UTC](https://forum.inductiveautomation.com/t/using-lambdas-in-gateway-event-scripts/19548/1 "2018-09-05T16:13:36Z")

</div>

I have a portion of code I’m running in a gateway event script (trimmed out for clarity):

```auto
if initialChange:
	from sys import exit
	exit(0)
tagPath = 'Daicel/DSSA_AZ/NB/A5/SN_Req/'
#Get the variables. Use a readAll for efficiency
tags = ['Year', 'Type', 'SN']
vals = map(lambda x: tagPath + x, tags)
logger.debug("vals: %s" % vals)
```

but I recieve this exception when it runs:

```auto
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 16, in File "", line 16, in NameError: global name 'tagPath' is not defined

```

However; this block of code runs without exception:

```auto
if initialChange:
	from sys import exit
	exit(0)
tagPath = 'Daicel/DSSA_AZ/NB/A5/SN_Req/'
#Get the variables. Use a readAll for efficiency
tags = ['Year', 'Type', 'SN']
vals = map(lambda x: 'Daicel/DSSA_AZ/NB/A5/SN_Req/' + x, tags)
logger.debug("vals: %s" % vals)
```

Also If I run the code in the script console, with the reference to tagPath, I don’t have a problem either.  
I don’t really understand why the gateway event script has a problem with the first block of code here…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 5, 2018, 4:16pm UTC](https://forum.inductiveautomation.com/t/using-lambdas-in-gateway-event-scripts/19548/2 "2018-09-05T16:16:41Z")

</div>

Gateway event scripts use legacy python scoping rules, presumably for backward compatibility reasons. The quickest fix is to move your code into a function in a project script module and call that function (with needed event local variables as arguments). The project script module will run with modern python scoping.

---

<div class="post-metadata">

**Author:** ![bdecelles](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bdecelles/32/5713_2.png) [@bdecelles](https://forum.inductiveautomation.com/u/bdecelles)\
**Post date:** [September 5, 2018, 4:18pm UTC](https://forum.inductiveautomation.com/t/using-lambdas-in-gateway-event-scripts/19548/3 "2018-09-05T16:18:35Z")

</div>

That would explain it, I have done this before, but I was calling a project scoped script from the gateway event. Thanks!
