
On Wed, May 26, 2021 at 2:00 AM Shreyan Avigyan pythonshreyan09@gmail.com wrote:
I'm aware that Python is a "Consenting Adults" language but I think Python should provide this functionality at least. This idea doesn't actually make it like Java. Because why should we mess around with that member anyway. Sometimes we want to provide members that users must use but mustn't change. Well if the user does, then "crash"! Suppose we have filename member and we must access that but if we change it by any chance then what will happen is Python will not close the file instead it will create a new file leaving it in an undefined state - "Half written half not".
At best, Python could check this at run time, after it's too late. Type annotations and a tool like mypy can check it at, effectively, compilation time. All you have to do is run mypy on your code, and you'll know if that private member got assigned to. That's its point!
What advantage would be gained by adding this functionality to the language interpreter, as opposed to being in the typing module and verified by an external tool? Do you really need your validation to be done later and in an unreliable way? There are four points at which a bug could be detected:
1) Editing 2) Deployment 3) Compilation (in Python's case, that happens on first import of the module) 4) Execution (an exception when you actually attempt the mutation)
#1 is achieved by ALL_CAPS_NAMING_CONVENTION, because if anyone attempts to assign to it, it's obvious that something's wrong. That's the absolute earliest that you could possibly notice the bug, and *we already have that feature* built into our own brains.
#2 would be where MyPy would catch the bug. Include a type checker pass in your pre-deployment checklist, possibly enforced by your pull request system, or verified automatically as part of CI.
I'm not sure how you could catch this kind of thing at phase #3, but it might be possible.
#4 is where you'd actually catch it, if your proposal were to be implemented. The mutation would have to occur, and only then would you get the exception. Possibly bombing your server in production.
We already have options 1 and 2 available to us. You can use something that a type checker can verify, you can use something that every human can verify, and you can even do both if you're paranoid. Do you really need more than that?
Remember: The thing that you declare Final will, some day, need to be changed. Probably as part of your own test suite (you do have one of those, right?). With MyPy, you could tell it not to validate that line, or that file. With a naming convention, you can explain the need for the change with a comment. How are you going to override the language feature?
ChrisA