# how to create a global variable in python function

**URL:** https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822
**Category:** General
**Tags:** function-node
**Created:** [23 January 2024 07:52 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822 "2024-01-23T07:52:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Kleisi](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@Kleisi](https://discourse.nodered.org/u/Kleisi)
#### Post date: [23 January 2024 07:52 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822/1 "2024-01-23T07:52:59Z")

</div>

Hello  
Can someone tell me how to create a global variable in a Python function

node-red-contrib-python-function  
Node Red V3.1.3

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [23 January 2024 07:58 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822/2 "2024-01-23T07:58:45Z")

</div>

Hello @Kleisi, welcome to the forum.

Can you explain why you want to do that?  
Would it work to set the variable in a change node using the output of your python node?

---

<div class="post-metadata">

### Author: ![Kleisi](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@Kleisi](https://discourse.nodered.org/u/Kleisi)
#### Post date: [23 January 2024 08:13 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822/3 "2024-01-23T08:13:31Z")

</div>

Hello

I want to count signals or use calculations in other Python functions in other flows

and because I'm just interested 🙂

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [23 January 2024 08:46 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822/4 "2024-01-23T08:46:09Z")

</div>

I could be wrong,

But what you are trying to achieve is not possible.

Each python function node creates a new python process.

You don’t have the likes of global variables with this approach.

Like @jbudd says - use the Node RED globals to pass values to and from the python scripts.

But remember - the scripts are not in the same process as they are in Node RED

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 January 2024 10:11 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822/5 "2024-01-23T10:11:24Z")

</div>

Or replace the python functions with node red flows.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [23 January 2024 12:27 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822/6 "2024-01-23T12:27:58Z")

</div>

> [@Kleisi](#):
>
> I want to ... use calculations in other Python functions in other flows

There is a valuable and little known feature in Node-red for passing data between different flow tabs and internal & external processes: messages.

You can think of a message like a railway train. It travels along a prescribed track. At every stop different people and luggage can join the train and they are carried safely onward to their destination.

> [@Kleisi](#):
>
> I want to count signals

A context variable is a viable way to do this. But global context is ridiculous overkill. Use a variable whose scope is restricted to a flow or even a single node and _put that value on the train_ for use further down the track.

A little javascript function to achieve this:

```auto
let signalCount = context.get("signalCount") || 0
signalCount += 1
context.set("signalCount", signalCount)
msg.signalCount = signalCount
return msg

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [23 March 2024 12:28 UTC](https://discourse.nodered.org/t/how-to-create-a-global-variable-in-python-function/84822/7 "2024-03-23T12:28:08Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
