lst = [1, 2, 3]
dct = {'a': 1, 'b': 2}
tpl = (1, 2, 3)
strng = 'abc'
dct_in_lst = [dct]
print('match variation 1')
for i in [lst, dct, tpl, strng, dct_in_lst]:
match i:
case []: print('list', i, type(i))
case {}: print('dict', i, type(i))
case (): print('tuple', i, type(i))
case '': print('string', i, type(i))
case [{}]: print('dictionary inside list', i, type(i))
print('\nmatch variation 2')
for i in [lst, dct, tpl, strng]:
match i:
case list(): print('list', i, type(i))
case dict(): print('dict', i, type(i))
case tuple(): print('tuple', i, type(i))
case str(): print('string', i, type(i))
outputs,
match variation 1
dict {'a': 1, 'b': 2} <class 'dict'>
dictionary inside list [{'a': 1, 'b': 2}] <class 'list'>
match variation 2
list [1, 2, 3] <class 'list'>
dict {'a': 1, 'b': 2} <class 'dict'>
tuple (1, 2, 3) <class 'tuple'>
string abc <class 'str'>
is there a specific reasoning that dictionaries get matched for partial/subpatterns also.