Re:Pattern matching accepted for Python
by coldtea on Thursday February 11, 2021 @02:44PM
(#26106156)
>Oops, I've accidentally written 'position.absolute' instead of 'neighbour.absolute'!
That's a contrived example though, if I ever saw one.
I don't think that's the kind of issue people commonly have, compared to misuse of shadowed variable further down the scope.
And for your example, a better solution would be for the close to declare what it wants to use from its environment. Python doesn't allow this syntax, but some languages do:
def neighbourhood(position):
return map(
lambda neighbour use (): EDGE if neighbour is None else position.absolute,
position.neighbours
)
Now the compiler can again warn you, since you're only allowed to use neighbour in the lambda.
That's a contrived example though, if I ever saw one.
I don't think that's the kind of issue people commonly have, compared to misuse of shadowed variable further down the scope.
And for your example, a better solution would be for the close to declare what it wants to use from its environment. Python doesn't allow this syntax, but some languages do:
def neighbourhood(position): return map( lambda neighbour use (): EDGE if neighbour is None else position.absolute, position.neighbours )
Now the compiler can again warn you, since you're only allowed to use neighbour in the lambda.