switch
An explicit switch statement for Python, implemented as a context manager.
Usage
switch()Use it in a with block: register cases with case() and default(), then read the matched case’s return value from result.
See https://github.com/mikeckennedy/python-switch for full details. Copyright Michael Kennedy (https://mkennedy.codes) License: MIT
Attributes
| Name | Description |
|---|---|
| result | The value returned by the function of the matched case. |
result
The value returned by the function of the matched case.
result: Any
When cases fall through, result is the return value of the last function executed.
value = 4
with switch(value) as s:
s.case(closed_range(1, 5), lambda: "1-to-5")
# ...
res = s.result # res == '1-to-5'
Raises
Exception- If accessed before the switch block has exited and computed a result.
Methods
| Name | Description |
|---|---|
| __enter__() | Enter the switch block. |
| __exit__() | Run the matched case (and any fall-through cases) as the block exits. |
| __init__() |
Create a new switch block that tests cases against value.
|
| case() | Register a case for the switch block: |
| default() | Register the default case: the action to run when no other case matches. |
__enter__()
Enter the switch block.
Usage
__enter__()Returns
switch-
The switch instance itself (bind it with
as sto register cases).
__exit__()
Run the matched case (and any fall-through cases) as the block exits.
Usage
__exit__(exc_type, exc_val, exc_tb)Raises
Exception- If no case matched the value and no default case was registered.
__init__()
Create a new switch block that tests cases against value.
Usage
__init__(value)Parameters
value: Any- The value each case key is compared against.
case()
Register a case for the switch block:
Usage
case(key, func, fallthrough=False) with switch(val) as s:
s.case('a', function)
s.case('b', function, fallthrough=True)
s.default(function)
Parameters
key: Any-
Key for the case test. If this is a list or range, each item is added as a case for
func. func: Callable[[], Any]-
Any callable taking no parameters, executed if this case matches.
fallthrough: bool | None = False-
Optionally fall through to the subsequent case (defaults to False).
Noneis reserved for internal use and leaves the fall-through state unchanged.
Returns
bool- True if this case (or any item of a list or range key) matched the switch value, otherwise False.
Raises
ValueError- If the key is a duplicate, the key is an empty collection, or func is not callable.
default()
Register the default case: the action to run when no other case matches.
Usage
default(func)Use it as the optional final statement in the switch block. Note that the ordering is not enforced: if default() is registered before a case that also matches, both will run. Always register it last.
with switch(val) as s:
s.case(...)
s.case(...)
s.default(function)
Parameters
func: Callable[[], Any]- Any callable taking no parameters, executed if no other case matched.
Returns
None- None