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 <
alkathiri.abdu...@gmail.com> 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?
>
> Thanks,
>
> Abdulla
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/HOFNVZ6KJA6SHCE4NCLPSK27XLDMK7VR/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ROVCZOFH7OXVI32VPS2K5SAF2FXUA3UN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to