on-internet
← Community

Python Pattern Matching with optional variables

Stack Overflow ·

I'm using mypy and I have two classes: class Student: first_name: str ... class City: id: int name: str ... I have some db function that fetch the data and I end up with these two variables: student: Optional[Student] = ... city: Optional[City] = ... I want to do pattern match for each case of None or non-None data of both variables. I want to do different things for each case, this way: match student, city: case None, None: output = "they all none" case Student(), City(): output = f"student: {student.first_name} lives in {city.name}" case _, _: output = "one of them is none..." But mypy doesn

Comments

Sign in or sign up to comment and upvote.

No comments yet.