Vyper theorytheory 0/50 · 0%
Storage · easy

5. Constants and immutables

constant vs immutable, and when to use each.

`constant` values are baked in at compile time and can never change; they must be simple literal expressions. `immutable` values are set exactly once, inside the constructor, and thereafter live in bytecode rather than storage — cheap to read forever after.

RATE: constant(uint256) = 5
TOKEN: immutable(address)

@deploy
def __init__(token: address):
    TOKEN = token

Neither costs an SLOAD to read, which makes both far cheaper than a regular storage variable for values that never (or rarely) change after deployment.

Check your understanding

  1. 1. When is a `constant` value fixed?

  2. 2. Where can an `immutable` be assigned?

  3. 3. Why prefer constant/immutable over storage for fixed values?