Overview

Python variables in Decipher can have different scopes depending on the way they are declared when scripting. There are three different types of variables that can be declared - global variables, local variables, and persistent variables.

Global Variables

Global variables will remain static across all respondents taking your survey, and should be used for either read-only purposes or reusable functions. Because their values are shared across respondents, changing values on a global variable will affect all respondents, not just the respondent who triggered the change.

Global variables need to be defined when the survey is first initialized and can be accessed from any point in the survey to use for logic, calculations, or piping.

Declaring global variables within extraVariablesor as a sample source variableattribute allows you to access them from any point in the survey for piping, calculations, or building logic. Global variables defined using these two methods are static and only take the value assigned in the respondent’s URL.

To assign new a new value to the extra variable, you will need to use the setExtra()function (assigning directly to the variable will have no effect).

Defined in an <exec> Block

To define global variables within an <exec> block, you will need to use the when attribute and set it to "init", "virtualInit", or "virtual"; whichever value you choose here depends on the behavior you require.

when="init"

Variables inside of <exec> blocks with when="init" are created only when the survey initially loads and are not accessible within virtual questions. Since these constant variables are defined in an <exec> block with the when="init"attribute, they can be accessed later in the survey when performing calculations.

For example, imagine you wanted to ask the respondent what price they are willing to pay for a product and use that price for an adjusted amount at a later question. First, you would define your constant variables using when="init":

when="virtualInit"

Variables inside of <exec> blocks with when="virtualInit" work exactly like those within when="init" blocks, but the variables are only accessible in virtual questions. Below is the previous example working in a virtual question:

Local Variables

Local variables are any variables defined in an <exec> block which does not have a when="init"/"virtualInit"/"virtual"attribute. These variables are only accessible within the tag in which they are declared.

For example, if you wanted to declare a local variable "counter" that gets updated if a row in a question (Q1) is selected, you would enter this into the <exec> block directly:

Persistent Variables

Persistent variables are variables that are unique per respondent, and once declared, exist in the survey until the respondent completes the survey or is deleted.

Persistent variables are stored in what is called a “persistent dictionary”, which makes them accessible from any point in a survey. This allows for the storing and piping of specific values that would otherwise need to be stored in a separate question. It also allows you to perform later calculations using the values stored here.

Persistent variables are not accessible in virtual questions or in the data, so we recommend only using them to store data that you know will not be accessed later.

Declaring a Persistent Variable

A persistent variable is declared in the same way as a regular variable, but with the addition of "p."in front of the variable name:

<exec>
p.myVariable = 5
</exec>

Declaring the variable using the above example allows you to access it later from anywhere in your survey. For example, you can later grab the value that you stored in your persistent variable to punch a single-select question:

If you are using a persistent variable for text piping, that variable must be declared as a string type (e.g., p.myVariable = "0").

Deleting a Persistent Variable

As persistent variables take up more system resources than regular variables, having too many of these in a survey could result in the survey experiencing reduced performance. In order to avoid this, a good practice is to delete persistent variables after they have been used in the survey. If you know that you won’t be using a persistent variable past a certain point in a survey, you can delete it as shown below:

<exec>
del p.myVariable
</exec>

Using p.markers

A persistent variable defined as "p.markers" is available by default in every survey without having to be defined. This variable stores markers assigned to respondents either by quotas, or by manually setting markers.Using this variable returns a list of all the markers assigned to a respondent, which can be used for logic conditions in the survey.