On Wed, May 26, 2021 at 02:09:58AM +1000, Chris Angelico wrote:
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?
That depends on how it is implemented. Here are some thoughts. Given:
constant spam = "spam and eggs"
we might be able to change it like this:
# Rebinding a constant raises a warning. Just ignore the warning. try: spam = "spam spam spam spam" except Warning: pass
# Write directly to the namespace. globals()['spam'] = "spam spam spam spam"
# Command line switch or environment variable. python3.11 --no-constants testsuite.py
# Context manager. with ConstantEnforcement(False): spam = "spam spam spam spam"
# Special backdoor. sys.setconstant('spam', "spam spam spam spam")
Enforcing constantness doesn't necessarily imply there is no way to disable or work around it. Especially in a consenting adults language like Python, I would expect that there will be.
(Most likely just write directly to the namespace.)