On Wed, May 26, 2021 at 11:57 AM Steven D'Aprano <st...@pearwood.info> wrote:
>
> 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.)
>

And the very presence of such an option means that these things are
not constant, meaning that the interpreter and the programmer cannot
assume they are constant. Especially if there's a way to globally
disable constness. (My guess is that writing directly to the
namespace, or "import some_module; some_module.spam = 42", would
bypass it.)

So how long will it be before some library has a thing declared as a
constant, and some user of that library overrides it in production?

I give it a week, but only because I doubt that const would be heavily
used. If it were, I'd give it a day.

ChrisA
_______________________________________________
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/QTKU3VI5JQT22BLYQC7HWTTWA44Q6QGP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to