Python Notes
Table of Contents
match/case
Pattern Matching on a Dictionary
def show_title(state): print("Title") print(f"Hi Score: {state['hi_score']}") state["screen"] = "gameplay" state["state"] = "playing" def gameplay(state): print("Gameplay") print(f"state: {state['state']}") print(f"score: {state['score']}") match state: case { "state": "playing", "score": score, } if score < 50: state["score"] += 10 case {"state": "playing"}: state["state"] = "paused" case {"state": "paused"}: state["screen"] = "quit" def game_loop(): state = { "screen": "title", "hi_score": 100, "score": 0, } while True: print(state) match state: case { "screen": "title", "hi_score": hi_score, } if isinstance(hi_score, int): show_title(state) case { "screen": "gameplay", "state": sub_state, } if sub_state in ["playing", "paused"]: gameplay(state) case {"screen": "quit"}: print("Goodbye") break
Regular Expressions
- Python Docs: Regular Expressions
- Regular expressions are bytecode compiled and executed by C code in Python