Lambdas
The type of functions in SmartPy is sp.lambda_(t1, t2, with_storage= "read-write"|"read-only"|"no-access", with_operations=True|False, with_exceptions=True|False, with_mutez_overflow=True|False, with_mutez_underflow=True|False)
where t1
is the argument type and t2
the result type. It corresponds to the lambda
type in Michelson.
Lambdas can be defined either using Python's lambda x: ...
syntax or, when they have a name, using def f(x): ...
.
For example, the definition
f = lambda x: x + 1
is equivalent to:
def f(x):
return x + 1
Here f
has type sp.lambda_[sp.int, sp.int]
.
To call a lambda, pass it its argument in parentheses. This example calls the lambda in the previous example:
assert f(1) == 2
Effects
You can control the effects a lambda can have on a contract's storage by adding the @sp.effects(...)
decorator to a function definition.