Booleans โ
The type used to represent SmartPy booleans is sp.TBool.
The corresponding type in Michelson is
Have also a look at Comparison Operators.
Literals โ
sp.bool(<boolean>)
Create a literal of type sp.TBool when boolean
is a Python bool literal (True
or False
).
True
, False
The usual way to input sp.TBool literals in SmartPy.
Example โ
value = True
# or
value = sp.bool(True)
Operations โ
Boolean negation โ
~expr
Return the negation of expr
, where expr
must be a boolean.
Example โ
sp.if ~expr:
#...
Or โ
expr1 | expr2
Return True
if expr1
or expr2
are True
. Both expr1
and expr2
must be boolean expressions.
Example โ
sp.if (expr1) | (expr2):
#...
And โ
expr1 & expr2
Return True
if expr1
and expr2
are True
. Both expr1
and expr2
must be boolean expressions.
Example โ
sp.if (expr1) & (expr2):
#...
Exclusive or โ
expr1 ^ expr2
Return True
if expr1
and expr2
are different. Both expr1
and expr2
must be boolean expressions.
Example โ
sp.if (expr1) ^ (expr2):
#...
INFO
Unlike in Python, &
and |
do short-circuiting on SmartPy boolean expressions.
For example, the evaluation of ((x==x)ย |ย (self.data.xs[2]ย ==ย 0))
will not fail.
WARNING
Please note that not
, and
, and or
cannot be overloaded in Python. Hence, we cannot use them to construct SmartPy expressions and, as is customary for custom Python libraries, we use ~
, &
, and |
instead.