无法理解python匹配大小写

问题描述 投票:0回答:1

为什么这两个示例给出了 2 个不同的结果?如果

str()
等于
''
,结果应该是相同的。

    # example 1
    match 'ciao':
        case str():
            print('string')
        case _:
            print('default')
    
    # >>> string
    
    # example 2
    match 'ciao':
        case '':
            print('string')
        case _:
            print('default')
    
    # >>> default
    
    # but ...
    assert str() == ''
python match case
1个回答
0
投票
在这种情况下,

str()
not 是一个计算结果为
''
的函数调用表达式。相反,它是一个类模式,一种仅存在于
match
语句内的语法结构,并且与其他看起来相同的表达式具有不同的语义。您可以在文档中阅读有关 类模式的更多信息,但简而言之,
str()
匹配 str
任何
实例。

© www.soinside.com 2019 - 2024. All rights reserved.