On Wed, May 26, 2021 at 10:37:34PM +1000, Chris Angelico wrote:
Also, what about this:
constant x = 10 def f(): x = 20
SyntaxError? Runtime error? Shadowing?
The x inside the function is just a local variable. The x outside the function and the x inside it are in different namespaces and shouldn't clobber each other any more than variables in different namespaces do.
Constants in statically typed languages are easy, because the compiler knows which names are constant at compile-time and can disallow assignments to those names without any runtime cost.
Constants in dynamically typed languages like Python are hard, because we either have to severely restrict their use so that they can be determined at compile-time, which goes against the dynamic nature of the language, or else we have to slow down every assignment to check whether it is a constant or not. So the irony is that constants can speed up statically typed languages, but slow down dynamically typed ones.