Date: Wed, 8 Jul 2020 20:00:44 +0300 From: Paul Sokolovsky <pmiscml@gmail.com> Subject: [Python-Dev] Re: Python is the only language with lack of
[ munch ]
Right. So, if someone would like to add something to this thread, I'd humbly suggest to concentrate on the lack of, and need for, of const-ness in the Python language core (in comparison to other languages or not), and usecases it enables, and not on criteria for "popularness".
My view is that the informal Python "don't mess with anything all in upper case" coding convention works at least as well as any more formal language notation for nearly all cases. It conveys intention very well with minimum effort, and does not require programmers to think about the language they're using rather than the problem they are trying to solve. I can see the case for a const annotation/keyword for simple scalar values, almost always numbers, but am dubious about the benefits. If someone really wants to write IMPORTANT_SIN30 = math.sin(45) there's only so much a compiler/interpreter can do to stop them. "If we just provide enough detail we can catch every error" is a mirage that's lured away so much effort for many decades. (That said, I'd be in favour of adding math.PI and similar names because even I find it slightly disturbing that I can write math.pi = 3.0 and it doesn't look wrong.) Even for simple numbers, constness can be argued. Is the value determined when the code is written, or when run? A lot of mutable variables in Pascal/C/Ada programs are really constants, but the value is "number of CPU cores" or something else that can't be determined until first use. So those other languages that supposedly have constness are actually often more like Python. Once you go beyond scalar values, it's just a mess. If I write const myd = { "answer" : 42 } do I mean that myd will always be a reference to that object, or that the object being pointed to has constant members? To see what can happen if you try and build this into the language, look at C++. It has the const keyword for objects and object references. But then the "mutable" keyword got added to describe object members, because programmers needed to be able to modify the internal values of "const" objects! So now "const" in C++ for objects just means you don't intend to change the internal state, but it might do so anyway. Again, I can see a case in embedded systems for "const" meaning "this can be put in ROM". But I don't think that use case is common enough to justify adding to the core language. -- cheers, Hugh Fisher