Pairs โ
Pairs in SmartPy are of type sp.TPair(t1
, t2
).
Literals โ
sp.pair(expr1, expr2)
or (expr1, expr2)
Define a pair of two elements.
Example โ
python
aPair = (1, "A String")
aPair2 = sp.pair(1, 2)
sp.tuple(expr1, expr2, expr3)
or (expr1, expr2, expr3)
Define a tuple with more than two elements.
Example โ
python
Michelson PAIRaPair = (1, "A String", 3)
aPair2 = sp.tuple(1, 2, "A String")
Operations โ
Get the first element โ
sp.fst(..)
Access the first element of a pair.
Example โ
python
aPair = (1, "A String")
sp.fst(aPair) # 1
Get the second element โ
sp.snd(..)
Access the second element of a pair.
Example โ
python
Michelson CARaPair = (1, "A String")
sp.snd(aPair) # A String
and
Michelson CDRMatch elements โ
sp.match_pair(x)
If x
is a SmartPy pair (i.e. a value of type sp.TPair(t
, u
)), this returns a Python pair of its components.
Example โ
python
x1, x2 = sp.match_pair(p)
See reference Match template.