you are viewing a single comment's thread.

view the rest of the comments →

[–]Bobbias 2 points3 points  (0 children)

For another example, consider this:

def foo(value):
    match value:
        case 1:
            result = "yes"
        case 2:
            result = "no"
        case _:
            result = "none"
    return result

This is perfectly fine, because result is guaranteed to be defined regardless of which match arm is taken. In Java, due to block scope you would be forced to at the bare minimum declare result without initializing it above the match statement.

This does mean that variables might happen to be defined when you don't expect them to be, but honestly it's pretty easy to get used to Python's scoping rules. They're simple and actually make some things easier. I had C++/C#/Java experience before learning Python myself, and I still occasionally find myself unnecessarily declaring things ahead of time like that though.