Constants
ASnake Documentation
Summary:
A variable marked as constant is a value at compile-time and runtime that should not be modified.
Syntax constant
:
Here is a list of it's syntax aliases:
- constant
- const
You can mark a variable as constant prior to a variable name like this:
constant x = 1
# or the alias
const y = 2
It can be mixed with type declarations, so long as its to the left of the variable name.
const float pi = 3.14159
int const myFavoriteNumber 12
The optimizer should not eliminate constant variables when at global scope, so that the constant variable can be called as a library variable from another script. It should also not perform constant propagation so that the variable is modifiable in the compiled Python source code.
# assume that the previous example is compiled to myFile.py
from myFile import myFavoriteNumber
print myFavoriteNumber
# result: 12
The variable may be optimized away when at local scope (functions, conditional blocks), as it will not break intended behavior.
Scroll to top