Match statement with literal strings

Jason Friedman jsf80238 at gmail.com
Wed Jun 7 18:18:14 EDT 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


More information about the Python-list mailing list