All, " And Python can't do that, and that's probably a good thing."
Johnathan, you are right, I emailed this list a year or so ago with a way to overload assignment and lookup (i.e. what happens when you >>> a) and it was discussed for a while, but ultimately there were reasons (that I largely agree with) why that would be problematic in python. The title was "A proposal (and implementation) to add assignment and LOAD overloading" for anyone wanting to read the reasoning. On Wed, Aug 5, 2020 at 2:13 PM Jonathan Goble <[email protected]> wrote: > On Wed, Aug 5, 2020 at 2:03 PM Ricky Teachey <[email protected]> wrote: > >> >> And btw this works: >> >> >>> class const(int): >> ... def __new__(cls, name, val): >> ... obj = super().__new__(cls, val) >> ... obj.name = name >> ... return obj >> ... def about(self): >> ... print(self.name, '=', self) >> ... >> >>> a = const('a', 5) >> >>> a >> 5 >> >>> a.about() >> a = 5 >> > > 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. > _______________________________________________ > Python-ideas mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/TCW3TAPS6WTHGENNL2G2M5DTCKQWRJE6/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Nate Lust, PhD. Astrophysics Dept. Princeton University
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/PGQUKE4HF3IE4DGYVF7PQGTDTGXFNLWP/ Code of Conduct: http://python.org/psf/codeofconduct/
