← Back to context

Comment by user453

3 days ago

That's destructuring, a feature not a bug. Same as it works in any functional language - and tremendously useful once you get the hang of it

At least functional languages tend to have block scope, so the latter snippet introduces a new variable that shadows `not_found` instead of mutating it.

No, at least in Erlang a variable is assigned once, you can then match against that variable as it can't be reassigned:

    NotFound = 404,
    case Status of
        NotFound -> "Not Found";
        _ -> "Other Status"
    end.

That snippet will return "Other Status" for Status = 400. The Python equivalent of that snippet is a SyntaxError as the first case is a catch all and the rest is unreachable.

Destructuring is a feature. Making it easy to confuse value capture and value reference was an error. Add single-namespace locals and you have a calamity.

Destructuring yes but you can still argue it's poorly designed. Particularly unintuitive because matching on a nested name e.g. module.CONSTANT works for matching and doesn't destructure. It's just the use of an unnested name which does destructuring.

What Python needs is what Elixir has. A "pin" operator that forces the variable to be used as its value for matching, rather than destructuring.

It wouldn't be a problem is Python had block level variable scope. Having that destructuring be limited to the 'case' would be fine, but obliterating the variable outside of the match is very unexpected.