元组示例

控制流 / 条件语句 / match 语句

def get_point():
    return 5, 5


def describe(point):
    match point:
        case (0, 0):
            return "(0, 0) point"
        case (_, 1):
            return "(_, 1), y == 1"
        case (1, y):
            return f"(1, {y}), x == 1"
        case (x, y) if x == y:
            return f"({x}, {y}), x == y"
        case _:
            return "other point"


text = describe(get_point())
# text is '(5, 5), x == y'

# A list fits; a longer tuple does not
list_text = describe([1, 7])
# list_text is '(1, 7), x == 1'
long_text = describe((1, 7, 3))
# long_text is 'other point'

print(f"{text = }")
print(f"{list_text = }")
print(f"{long_text = }")