https://github.com/philippeitis/swytch
Yet another switch-case implementation in Python.
https://github.com/philippeitis/swytch
Last synced: over 1 year ago
JSON representation
Yet another switch-case implementation in Python.
- Host: GitHub
- URL: https://github.com/philippeitis/swytch
- Owner: philippeitis
- License: mit
- Created: 2020-04-03T03:52:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-11T19:10:31.000Z (almost 6 years ago)
- Last Synced: 2025-01-21T23:34:07.581Z (over 1 year ago)
- Language: Python
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swytch
If you've ever felt like you really, really need a switch statement in Python, then this might be the thing for you.
There's a little bit of everything for everyone: Exhaustive switches, default cases, switch cases for hashable types, switch cases for unhashable types, context-based switches, and switches over enums.
```python
import enum
from xswitch import case, Default, ExhaustiveSwitch
class PowersOfTwo(enum.IntEnum):
zero = 1
one = 2
two = 4
class PowersOfTwoSwitch(ExhaustiveSwitch, enum=PowersOfTwo):
@case(PowersOfTwo.zero)
def zero(self):
print("2^0")
@case(PowersOfTwo.one)
def one(self):
print("2^one")
@case(Default)
def default(self):
print("2^?")
if __name__ == "__main__":
PowersOfTwoSwitch.match(0)
PowersOfTwoSwitch.match(PowersOfTwo.one)
PowersOfTwoSwitch.match(5)
>>> 2^0
>>> 2^one
>>> 2^?
```