Vyper theorytheory 0/50 · 0%
Storage · medium

29. self and module-level state

How self. distinguishes storage from locals.

Any reference to a state variable must be prefixed with `self.` — this makes it visually unmistakable whether a line touches persistent storage (expensive, permanent) or a local/memory variable (cheap, temporary).

total: public(uint256)

@external
def add(amount: uint256):
    local_copy: uint256 = self.total   # read storage into memory
    local_copy += amount
    self.total = local_copy            # write back to storage

This explicitness is one of Vyper's clearest departures from Solidity, where storage vs memory can sometimes be ambiguous at a glance (especially with struct/array references).

Check your understanding

  1. 1. What must prefix every reference to a state variable?

  2. 2. Why is this explicitness valuable?

  3. 3. Does a local variable need the self. prefix?