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: self.data.x = ...

Example:

smartpy
@sp.entrypoint
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)
    self.data.value = y

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

python
@sp.module
def main():
    class Calculator(sp.Contract):
        def __init__(self):
            self.data.result = 0

        @sp.entrypoint
        def multiply(self, x, y):
            self.data.result = 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:

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