• Match statement with literal strings

    From Jason Friedman@21:1/5 to All on Wed Jun 7 16:18:14 2023
    This gives the expected results:

    with open(data_file, newline="") as reader:
    csvreader = csv.DictReader(reader)
    for row in csvreader:
    #print(row)
    match row[RULE_TYPE]:
    case "RANGE":
    print("range")
    case "MANDATORY":
    print("mandatory")
    case _:
    print("nothing to do")

    This:

    RANGE = "RANGE"
    MANDATORY = "MANDATORY"
    with open(data_file, newline="") as reader:
    csvreader = csv.DictReader(reader)
    for row in csvreader:
    #print(row)
    match row[RULE_TYPE]:
    case RANGE:
    print("range")
    case MANDATORY:
    print("mandatory")
    case _:
    print("nothing to do")

    Gives (and I don't understand why):

    SyntaxError: name capture 'RANGE' makes remaining patterns unreachable

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Ewing@21:1/5 to Jason Friedman on Thu Jun 8 11:58:51 2023
    On 8/06/23 10:18 am, Jason Friedman wrote:
    SyntaxError: name capture 'RANGE' makes remaining patterns unreachable

    The bytecode compiler doesn't know that you intend RANGE
    to be a constant -- it thinks it's a variable to bind a
    value to.

    To make this work you need to find a way to refer to the
    value that isn't just a bare name. One way would be to
    define your constants using an enum:

    class Options(Enum):
    RANGE = "RANGE"
    MANDATORY = "MANDATORY"

    match stuff:
    case Options.RANGE:
    ...
    case Options.MANDATORY:
    ...

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to python-list@python.org on Thu Jun 8 10:01:55 2023
    On Thu, 8 Jun 2023 at 08:19, Jason Friedman via Python-list <python-list@python.org> wrote:

    This gives the expected results:

    with open(data_file, newline="") as reader:
    csvreader = csv.DictReader(reader)
    for row in csvreader:
    #print(row)
    match row[RULE_TYPE]:
    case "RANGE":
    print("range")
    case "MANDATORY":
    print("mandatory")
    case _:
    print("nothing to do")

    This:

    RANGE = "RANGE"
    MANDATORY = "MANDATORY"
    with open(data_file, newline="") as reader:
    csvreader = csv.DictReader(reader)
    for row in csvreader:
    #print(row)
    match row[RULE_TYPE]:
    case RANGE:
    print("range")
    case MANDATORY:
    print("mandatory")
    case _:
    print("nothing to do")

    Gives (and I don't understand why):

    SyntaxError: name capture 'RANGE' makes remaining patterns unreachable

    It's being as clear as it can. When you say "case RANGE:", that is not
    a literal, that is a name capture. Check the docs and examples for
    case statements for more details.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jason Friedman@21:1/5 to python-list@python.org on Wed Jun 7 18:44:53 2023

    The bytecode compiler doesn't know that you intend RANGE
    to be a constant -- it thinks it's a variable to bind a
    value to.

    To make this work you need to find a way to refer to the
    value that isn't just a bare name. One way would be to
    define your constants using an enum:

    class Options(Enum):
    RANGE = "RANGE"
    MANDATORY = "MANDATORY"

    match stuff:
    case Options.RANGE:
    ...
    case Options.MANDATORY:
    ...


    Got it, thank you.

    On Wed, Jun 7, 2023 at 6:01 PM Greg Ewing via Python-list < python-list@python.org> wrote:

    On 8/06/23 10:18 am, Jason Friedman wrote:
    SyntaxError: name capture 'RANGE' makes remaining patterns unreachable

    The bytecode compiler doesn't know that you intend RANGE
    to be a constant -- it thinks it's a variable to bind a
    value to.

    To make this work you need to find a way to refer to the
    value that isn't just a bare name. One way would be to
    define your constants using an enum:

    class Options(Enum):
    RANGE = "RANGE"
    MANDATORY = "MANDATORY"

    match stuff:
    case Options.RANGE:
    ...
    case Options.MANDATORY:
    ...

    --
    Greg


    --
    https://mail.python.org/mailman/listinfo/python-list


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)