Код: Выделить всё
elif/else
Код: Выделить всё
case конструкция. Однако вроде совпадает/case
Код: Выделить всё
case
Я пробовал следующее код, но вместо этого возвращает ошибку:
Код: Выделить всё
import re
def match_pattern(string):
match string:
case re.compile(r' *if +(? *.+ *)? *: *'):
return 'if'
case re.compile(r' *for +\w+ +in +(? *.+ *)? *: *'):
return 'for'
# and other cases
case _:
return 'invalid'
if __name__ == '__main__':
print(match_pattern('if (a == 0):'))
print(match_pattern(' for i in l:'))
Код: Выделить всё
Traceback (most recent call last):
File "match_case.py", line 16, in
print(match_pattern('if (a == 0):'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "match_case.py", line 6, in match_pattern
case re.compile(r' *if +(? *.+ *)? *: *'):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: called match pattern must be a type
Подробнее здесь: https://stackoverflow.com/questions/787 ... xpressions