Variables & Scope

Binding names to values, and where those names live

A variable is a name bound to a value with =. Assignment is a statement: it runs for its effect on the current scope, not for a value.

Quick reference

x = 1              # bind a name
list[0] = 9        # assign into a list element
p.x = 42           # assign a property

Binding a variable

The left-hand side is a name, the right-hand side is any expression:

x = 1
greeting = 'hello'
total = x + 10

Reusing a name rebinds it to the new value.

Index assignment

Assign into a list element (or any indexable value) by targeting the index:

list = [1, 2, 3]
list[0] = 9

Property assignment

Assign to a property with dotted access on the left-hand side:

p.x = 42

Scope

A name is visible from its binding onward within the enclosing scope. Loop variables are scoped to their loop - once the loop ends the loop variable is unbound. A plain read of an unbound name raises an "unknown variable" error; append ? to read it leniently as <undefined> instead:

for x in 'foo' index i
  print(i)
  print(x)
end
print(x?)

The trailing print(x?) sees x as <undefined>, because the loop binding does not escape the loop. Without the ?, a plain print(x) would raise an unknown variable "x" error.

See also