The solution here is not to use the same name for two different concepts in the first place. Python doesn't have scopes associated with blocks, only with functions, and occasionally this means you have to pick your names carefully -- the same happens e.g. if you reuse a variable as a for-loop control variable.

You can say that you don't want failed cases to bind any variables -- but what if the pattern succeeds and now a guard (the 'if' clause) needs to check the variable? We went down the route of how to make this work and in the end decided it would be too complicated.

Or what if a case succeeds, and then after the match statement is over and done with (not in `case _:`) you still wanted access to the global 'name'? The long and short of it is that match statements are liable to any or all names bound in any of the patterns used, and your code is incorrect if you aren't prepared for that.

A variant of the syntax to allow alternate semantics sounds worse.

On Fri, Feb 12, 2021 at 10:30 PM Abdulla Al Kathiri <> wrote:
I will explain it in the following few lines of code..

name = "George"
year = 2021

d = {"name": "Mike", "year": 1919}

match d:

        case {"name": name, "year": 1917}:
                print("match 1 found”)
                # I want to remove binding "name" here from partial matching

        case {"year": year, "name": "Michael”}:
                print("match 2 found”)
                # I want to remove binding "year" here from partial matching.
                # Basically removing all name bindings after every partial/failed matching

        case _:
                print("match not found!”)
                print(f"{name = }, {year = }”)

### Output ###:
match not found!
name = 'Mike', year = 1919

But I want :var: 'name' to stay being the global name “George" and :var: 'year' being the global year 2021 if an exact matching is not found.

Maybe it is done the way it is for speed optimization (overhead reduction or something), but what if I don't care about speed and I care about having no un-intentional side-effects?

Can we do something like the following to solve this issue?
match d, partial_binding=False:
        case … : ...
        case … : ...
with partial_binding=True by default.

Any other idea in how to prevent name binding due to partial matching from happening? Any previous discussions on this?


Python-ideas mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct:

--Guido van Rossum (
Pronouns: he/him (why is my pronoun here?)