Structural matching simplifies the code


Well known structural matching in python would be match case. But there’s an other kind and it checks # of items, e.g.,

[a] = foo()

If foo() returns [1], it works fine. But if it returns either multiple items [1, 2, 3] or an empty list, it fails. This is pretty neat. I have been checking the length of returned list with assert. Above code remove such needs.


Leave a Reply

Your email address will not be published. Required fields are marked *