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.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OREZTG4ZKJS24DSU2Z2BAUOUICRYIQRH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to