[英]How can I make VSCode recognize structural pattern matching (match case) with Python3.10?
[英]How to access the matched value in the default case of structural pattern matching?
使用 Python 3.10 的 match 語句,是否可以使用默認情況下遇到的值?
或者這是否需要在match
之前分配一個變量以便可以在默認情況下使用?
match expensive_calculation(argument):
case 'APPLE':
value = 'FOO'
case 'ORANGE':
value = 'BAR'
case _:
raise Exception(
"Wrong kind of fruit found: " +
str(expensive_calculation(argument))
# ^ is it possible to get the default value in this case?
)
您可以使用as
模式:
match expensive_calculation(argument):
case 'APPLE':
value = 'FOO'
case 'ORANGE':
value = 'BAR'
case _ as argument: #here, using `as` to save the wildcard default to `argument`
raise Exception(f"Wrong kind of fruit found: {str(argument)}")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.