gpt4 book ai didi

python - 使用结构模式匹配反转测试

转载 作者:行者123 更新时间:2023-12-05 04:32:35 24 4
gpt4 key购买 nike

我一直在将 if-elif-chains 转换为结构模式匹配,但在反向测试方面遇到困难。

创建匹配任何支持模式(文字、类、映射、序列等)的案例很容易。如何为否定匹配提出理由?

例如,当对象的类型匹配时,我需要强制对象:

   if isinstance(x, (list, dict)):
x = json.dump(x)
elif not isinstance(x, str): # <-- Inverted test
x = str(x)

最佳答案

基本技术

从本质上讲,结构模式匹配的设计只会在存在正匹配时触发一个案例。但是,有两种解决方法。

最简单的方法是添加一个 guard表达。但这是最后的手段,因为它没有利用模式匹配和解构功能。

第二种方法是添加一个较早的匹配测试,以便后面的情况可以假定反向匹配为真。如果否定案例是最后一个案例,那么这很好用。如果不是,它会变得有点尴尬,因为需要嵌套。

守卫

进行倒排测试并将其移动到 if 表达式中:

   match x:
case list() | dict():
x = json.dump(x)
case _ if not isinstance(x, str): # <-- Inverted test
x = str(x)

预测试

这里的基本思想是进行正匹配,以便后续案例可以假设为负匹配:

   match x:
case list() | dict():
x = json.dump(x)
case str(): # <-- Positive match
pass
case _: # <-- Inverted case
x = str(x)

用后续案例进行预测试

预测试技术很优雅,除非你有额外的案例要匹配:

   if isinstance(x, (list, dict)):
x = json.dump(x)
elif not isinstance(x, str): # <-- Inverted test
x = str(x)
elif x == 'quit': # <-- Additional test
sys.exit(0)

这里最简单的解决方案是将附加测试移到倒置测试之前:

   match x:
case list() | dict():
x = json.dump(x)
case 'quit': # <-- Moved the test up
sys.exit(0)
case str(): # <-- Positive match
pass
case _: # <-- Inverted case
x = str(x)

并非总是可以重新排序测试。如果是这样,则可以引入新级别的嵌套匹配:

   case list() | dict():
x = json.dump(x)
case str(): # <-- Positive match
match x: # <-- Nested match
case 'quit': # <-- Inner case
sys.exit(0)
case _: # <-- Inverted case
x = str(x)

关于python - 使用结构模式匹配反转测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71629831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com