On Wed, Aug 5, 2020 at 3:40 PM Ethan Furman <ethan@stoneleaf.us> wrote:
On 8/5/20 11:11 AM, Jonathan Goble wrote:

> That's literally useless, because after running that there is nothing
> stopping you from doing:
>
>  >>> a = 10
>
> or even:
>
>  >>> a = "python has no constants"
>
> And now a has a value different from 5.
>
> There is nothing even remotely resembling const-ness to that class. In
> order to get const-ness, you would need the ability to overload
> assignments, like C++ can do. And Python can't do that, and that's
> probably a good thing.

     --> from aenum import Constant

     --> class K(Constant):
     ...   a = 5
     ...   b = 'hello'
     ...

     --> K.a
     <K.a: 5>

     --> K.a == 5
     True

     --> K.a - 3
     2

     --> K.a = 9
     Traceback (most recent call last):
       ...
     AttributeError: cannot rebind constant <K.a>

     --> del K.a
     Traceback (most recent call last):
       ...
     AttributeError: cannot delete constant <K.a>

However, one can, of course:

     del K

There is only so much one can do.  ;-)


Actually, it is possible to do with Python, using an import hook. See https://aroberge.github.io/ideas/docs/html/constants.html 

André Roberge

 
--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/VUZKCKX7CARMZI3BNDQRGA7U4HHDXDMP/
Code of Conduct: http://python.org/psf/codeofconduct/