Preprocessing code in match block

I was working on something when I felt the need to use some structural pattern matching features in 3.10. I’ve used the feature before but not for something production level. Anyways, I felt the need to write something similar to this:

def checker(word:tuple[str,...]) -> None:
    match word:
        hashed = hash_func(word)
        case "pwd-",*body:
            #perform some operation using hashed and word
            process(word,hashed)
        case _:
            pass

but a syntax error was raised. It’s not something so big and I’m sure it’s avoidable, but I just thought it’d be more of a convenience if there were some bit of code you could run in the match block before running the cases.

Why don’t you just compute hashed before the match statement?

hashed = hash_func(word)
match word:
    ...

You can still access hashed from inside the match block.

2 Likes

Yes you could. It’s not really a feature I’m asking for since there are so many ways you could go around it.
I just thought it could add some bit of convince or something.

But generally I’m already used to how matching works now already, so no need to change anything. I was just thinking out loud when I suggested that :sweat_smile: