isacolak / python_switch

Python Switch Like JavaScript Switch Statement

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python Switch Like JavaScript Switch Statement

Installing

Install and update using pip:

pip install python-switch

A Simple Examples

from python_switch import Switch

s = Switch({"d":lambda x:f"returns {x} (d)","default":lambda x: f"returns {x} (default)"})

print(s.get("d")(1))

Adding a case later.

from python_switch import Switch

s = Switch({"default":lambda x: f"returns {x} (default)"})

s.addCase("d",lambda x:f"returns {x} (d)")

print(s.get("d")(1))

Adding cases with the decorator.

from python_switch import Switch

s = Switch({"default":lambda x: f"returns {x} (default)"})

@s.case()
def d(x):
 return f"returns {x} (d)"

print(s.get("d")(1))

About

Python Switch Like JavaScript Switch Statement

License:MIT License


Languages

Language:Python 100.0%