Else class¶
This page explains the Else
class.
Before reading on, maybe it is helpful to read the following page (the apysc uses the Else
class for the same reason as each apysc data type):
What is the Else class?¶
The Else
class is the apysc branch instruction class. It behaves like the Python built-in else
keyword.
Basic usage¶
The Else
requires using the with
statement. The Else
class statement is only acceptable to implement right after the If
or Elif
classes statement.
import apysc as ap
ap.Stage()
condition: ap.Boolean = ap.Boolean(False)
int_1: ap.Int = ap.Int(10)
with ap.If(condition):
int_1 += 10
with ap.Else():
int_1 += 20
Notes¶
If you insert the code between the If
(or Elif
) and Else
statements, it raises an exception:
import apysc as ap
ap.Stage()
condition: ap.Boolean = ap.Boolean(False)
int_1: ap.Int = ap.Int(10)
with ap.If(condition):
int_1 += 10
# If there is a code implementation between the `If` and `Else`, then
# exceptions will be raised.
int_2: ap.Int = ap.Int(20)
with ap.Else():
int_1 += 20
ValueError: Else interface can only use right after If or Elif interfaces.
See also¶
Else constructor API¶
Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.
[Interface signature] __init__(self, *, locals_: Union[Dict[str, Any], NoneType] = None, globals_: Union[Dict[str, Any], NoneType] = None) -> None
[Interface summary]
A class to append else branch instruction expression.
[Parameters]
locals_
: dict or None, default NoneCurrent scope’s local variables. Set locals() value to this argument. If specified, this interface reverts all local scope VariableNameMixIn variables (like Int, Sprite) at the end of an
Else
scope. This setting is useful when you don’t want to update each variable by implementing theElse
scope.
globals_
: dict or None, default NoneCurrent scope’s global variables. Set globals() value to this argument. This setting works the same way as the locals_ argument.
[Notes]
・You can only use this class immediately after the If
or Elif
statement.
[Examples]
>>> import apysc as ap
>>> int_val: ap.Int = ap.Int(10)
>>> with ap.If(int_val >= 11):
... ap.trace("Value is greater than equal 11.")
...
>>> with ap.Else():
... ap.trace("Value is less than 11.")
...
[References]