On 1 Jul 2020, at 18:54, Brandt Bucher wrote:

Walter Dörwald wrote:
This looks strange to me. In all other cases of variable lookup the global variable z would be found.

The next case assigns to z, making z local to whereis. This is consistent with python's existing scoping rules (for example, try rewriting this as the equivalent if-elif chain and you'll get the same error). It sounds like you want to add "global z" to the top of the function definition.

whereis(23) however works.

This branch is hit before the unbound local lookup is attempted.

OK, understood.

However I still find the rule "dotted names are looked up" and "undotted names are matched" surprising and "case .constant" ugly.

A way to solve this would be to use "names at the top level are always looked up".

With this the constant value pattern:

        case .name:

could be written as:

        case name:

The capture pattern (of which there can only be one anyway) could then be written as:

        case object(name):

instead of

        case name:

Or we could use "matches are always done against a match object", i.e. the code from the example would look like this:

    from dataclasses import dataclass

    @dataclass
    class Point:
        x: int
        y: int

    z = 42

    def whereis(point):
        w = 23
        match point as m:
            case Point(0, 0):
                print("Origin")
            case Point(0, m.y):
                print(f"Y={m.y}")
            case Point(m.x, 0):
                print(f"X={m.x}")
            case Point():
                print("Somewhere else")
            case w:
                print("Not the answer")
            case z:
                print("The answer")
            case object(z):
                print(f"{z!r} is not a point")

Servus,
   Walter
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/SPMTCDIRJJGCKSIPWE4EX6DSIBFXLAL4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to