# Assigning a Function to a Variable and then calling it

**URL:** <https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689>\
**Category:** Ignition\
**Created:** [May 17, 2022, 7:32pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689 "2022-05-17T19:32:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicholas.robinson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nicholas.robinson/32/22188_2.png) [@nicholas.robinson](https://forum.inductiveautomation.com/u/nicholas.robinson)\
**Post date:** [May 17, 2022, 7:32pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689/1 "2022-05-17T19:32:30Z")

</div>

What is the correct way to call a function that has been assigned to a variable? I want to call a specific function depending on a parameter like this:

**Decide Function Based on Column Name**

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/e/ce3e4417920efb4cbcb3e1697dfe558ded2d2fc4.jpeg)

**Assigned to the variable it looks like this**

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/7/a7096b5e2529981ccc4cfb6cad9e0be0e33e3957.png)

**I attempt to use it like this**

```auto
count, ds = self.custom.searchFunction(combinedDs, returnDs=True)

```

**This Error Happens**

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/e/7e48d13cab564ab002138322cd8fdcbc812ef286.jpeg)

Thanks,

Nick

---

<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:** [May 17, 2022, 7:33pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689/2 "2022-05-17T19:33:50Z")

</div>

When you assigned it to a Perspective property, it was stringified. Perspective properties cannot hold arbitrary jython objects.

---

<div class="post-metadata">

**Author:** ![nicholas.robinson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nicholas.robinson/32/22188_2.png) [@nicholas.robinson](https://forum.inductiveautomation.com/u/nicholas.robinson)\
**Post date:** [May 17, 2022, 7:35pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689/3 "2022-05-17T19:35:54Z")

</div>

ok, if that is something that I cannot change, I’ll just choose which function to call at the time of execution instead of trying to stick it in a variable.

There is no way to overcome this “stringification” correct?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 17, 2022, 7:41pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689/4 "2022-05-17T19:41:22Z")

</div>

You’re basically already making a map/dictionary. I assume you use this on some button press or something but in your `onAction` extension function (or wheverever, doesn’t really matter) you can do something like

```auto
funcs = {
"recircCount": common.flexsort.getRecircCount,
"overflowCount": common.flexsort.getOverflowCount,
"redLightCount": common.flexsort.getRedLightCount,
...
}

# some logic to figure out what key to use
keyToUse = ?

try:
    funcs[keysToUse]()
except KeyError:
    # Opportunity to use a fallback function if keyToUse is not in dictionary
    someDefaultFunc()

```

If you need to call this function mapping function in multiple places, you could also put it in a project script like

```auto
def callMappedFunction(keyToUse, *args, **kwargs):
    funcs = {
    "recircCount": common.flexsort.getRecircCount,
    "overflowCount": common.flexsort.getOverflowCount,
    "redLightCount": common.flexsort.getRedLightCount,
    ...
    }
    try:
        return funcs[keyToUse](*args, **kwargs)
    except KeyError:
        return defaultFunction(*args, **kwargs)

```

and call this where you need it on your view.

There is a way to go from the string name of your library to importing/calling the function using importlib I believe, but would probably recommend against it and just hardcode/white list what functions you want to allow.

---

<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:** [May 17, 2022, 7:43pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689/5 "2022-05-17T19:43:00Z")

</div>

Brian’s approach is good, but `funcs` needs to be at the top level of a project script to be most efficient.

---

<div class="post-metadata">

**Author:** ![nicholas.robinson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nicholas.robinson/32/22188_2.png) [@nicholas.robinson](https://forum.inductiveautomation.com/u/nicholas.robinson)\
**Post date:** [May 17, 2022, 7:43pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689/6 "2022-05-17T19:43:35Z")

</div>

I will try this and update the results here later on

---

<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:** [May 17, 2022, 7:44pm UTC](https://forum.inductiveautomation.com/t/assigning-a-function-to-a-variable-and-then-calling-it/59689/7 "2022-05-17T19:44:06Z")

</div>

> [@nicholas.robinson](#):
>
> There is no way to overcome this “stringification” correct?

Not at the moment, no. There may be hope for the future, though. (:
