I prefer specifying the type because I found one example, where not specifying the type would create an issue, that is in the case of list vs tuple,
tpl = (1, 2, 3)
match tpl:
case [1, 2, 3]: print(1)
case (1, 2, 3): print(2)
gives, 1
as output
so, I specify the type,
tpl = (1, 2, 3)
match tpl:
case list([1, 2, 3]): print(1)
case tuple((1, 2, 3)): print(2)
which gives 2
as expected.
so I prefer case float(0.5)
or case int(10)
to case 0.5
or case 10
, otherwise it is like specify type at some places and dont specify type at other places.
regarding the Decimal()
class, I have recently started using it, so dont really know that much about it, and it appears that for some reason Decimal(3.1)
gives, Decimal('3.100000000000000088817841970012523233890533447265625')
and most of the operators do not work when using Decimal
with other types, like Decimal * {float/complex/Fraction}
is all invalid.
maybe if both, x == z
and repr(x) == repr(z)
hold True
then it should be a match.
that is,
x = Decimal('3.10')
z = Decimal('2.05') + Decimal('1.05')
x == z, repr(x) == repr(z)
gives,
(True, True)
one more thing is that operators in certain places are not valid in case statements, that is,
x = 'ab'
match x:
case 'a' + 'b': print(1)
and,
x = 5
match x:
case 2 + 3: print(1)
both give error, so I would prefer case complex(real=1, imag=2)
over case 1 + 2j
, otherwise it is again like here it understands +
is for complex numbers, here it does not understand that +
is for string concatenation / addition of integers.