Skip to content
On this page

New syntax

With version 0.17.0 SmartPy introduces a new syntax. This allows users to write code in a syntax even closer to Python. Highlights:

  • No more sp.if, sp.for, sp.while. Just write if, for, while instead.

  • No more x = sp.local("x", ...) and x = sp.compute(...). Just write x = ... instead.

  • No more x.value to access a variable's value. Just write x instead.

  • Support for list comprehensions: [x+1 for x in xs].

  • A contract's __init__ can now be written in a more Python-like style with assignments: = ...


def squareRoot(self, x):
    assert x >= 0
    y = x
    while y * y > x:
        y = (x / y + y) / 2
    assert y * y <= x and x < (y + 1) * (y + 1) = y

Contracts are now part of modules, which are marked by the @sp.module decorator:

def main():
    class Calculator(sp.Contract):
        def __init__(self):
   = 0

        def multiply(self, x, y):
   = x * y

Note that in __init__ storage fields are assigned to, in contrast to the call to self.init in the old syntax.

The module (here main) needs to be given as an argument to sp.test_scenario:

@sp.add_test(name = "Calculator")
def test():
    scenario = sp.test_scenario(main)
    c = main.Calculator()
    scenario += c