# How to initialize a variable which won't be refreshed

I am coding a node for animation nodes. I need to initialize a set of variables so I can use them, but I need to do it in such a way that they won't be refreshed (reset to the default value) every time I send an output to the node.

I probably want to define the variables before my create(self) method, so it won't refresh every time I send/receive I/O from the execute(self,a,b,...) method.

I have this:

def create(self) new.Input(...) new.Input(...) new.Output(...) etc...

def execute(self,a,b,etc..)
loops involving variables.


Where do I define my variables, and what is the syntax for calling them to check or change values without resetting them to their default start settings?

You can define such variable outside of the node class and call it as you would call a global variable. Take the Text File Reader node for example, it defines a cache dictionary to cache file reads, the cache is initially defined outside of the class as:

cache = {}

...


And it assign and read the cache in the execute function normally:

with open(path, "r", encoding = encoding) as f:
cache[key] = (lastModification, data)
...


You can also use the node identifier as a key in the dictionary to have a per-node variables. For instance, if you want a variable xBounce for every node, create an empty dictionary called xBounce and in the execution function, set its value using xBounce[self.identifier] = something and retrieve its value using xBounce.get(self.identifier, 0) where zero is the default value

• I tried declaring three integer variables before the node itself, as you have shown here, but I still get a referenced before assignment error. I don't know how to show my code here in the comments, or I would add that to this question." Can you suggest what is going wrong? – NullNaught Nov 29 '18 at 13:31
• @NullNaught Please add the code to the question. – Omar Emara Nov 29 '18 at 14:30
• When I try to copy and paste, I get "866 characters too long." I don't know how to append my screenshot. I will try adding it as an edit to my question. – NullNaught Nov 29 '18 at 14:40
• @NullNaught It seems Python assumes the variable is in the local scope. You should probably declare it as a global variable, see this answer. – Omar Emara Nov 29 '18 at 15:13
• Can you tell me, is there any way to achieve the same thing without a global variable? I can't use two such nodes at the same time, when I would like to be able to. – NullNaught Nov 30 '18 at 7:47