exhaustive match python
- User
exhaustive match python
Assistant
Here's an exhaustive match pattern in Python using the structural pattern matching feature introduced in Python 3.10:
def describe_value(value): match value: case None: return "It's None" case bool(): return f"It's a boolean: {value}" case int(): return f"It's an integer: {value}" case float(): return f"It's a float: {value}" case str(): return f"It's a string: {value}" case list(): return f"It's a list with {len(value)} items" case dict(): return f"It's a dictionary with {len(value)} keys" case _: return f"It's something else: {type(value).__name__}" # Example usage print(describe_value(None)) print(describe_value(True)) print(describe_value(42)) print(describe_value(3.14)) print(describe_value("hello")) print(describe_value([1, 2, 3])) print(describe_value({"a": 1, "b": 2})) print(describe_value(set([1, 2, 3])))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.