Quick tip on testing existence of a variable...

Forum rules

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo

Posted on
Sun May 20, 2012 2:32 pm
Swancoat offline
Posts: 503
Joined: Nov 20, 2009
Location: Houston

Quick tip on testing existence of a variable...

I'm writing a plugin and as data comes in, for some of it, I want to update it in a variable. Now, my plan is to have the plugin create the variable, and then update it, but ideally, I want to test if it's already there (from running the plugin previously...), and if not, THEN create it.

I'm thinking this will work...
Code: Select all
      try:
         newvar = indigo.variables["MyVarName"]
      except:
         newvar = indigo.variable.create("MyVarName")
         


But I'm wondering if something like this might do it too..
Code: Select all
if "MyVarName" is in indigo variables[]:
logic
logic


Any thoughts on which of these is preferred?

http://nerdhome.jimdo.com

Posted on
Sun May 20, 2012 2:43 pm
matt (support) offline
Site Admin
User avatar
Posts: 21429
Joined: Jan 27, 2003
Location: Texas

Re: Quick tip on testing existence of a variable...

Yes, the preferred technique would be:

Code: Select all
if "myVarName" is not in indigo.variables:
  someVar = indigo.variable.create("MyVarName")
else:
  someVar = indigo.variables["myVarName"]

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest