On Wed, Aug 5, 2020 at 1:54 PM David Mertz <[email protected]> wrote:
> On Wed, Aug 5, 2020 at 1:27 PM Ricky Teachey <[email protected]> wrote:
>
>> On Wed, Aug 5, 2020 at 11:41 AM Marco Sulla <[email protected]>
>> wrote:
>>
>>> On Wed, 5 Aug 2020 at 15:53, Ricky Teachey <[email protected]> wrote:
>>> from mypython import *
>>> @const a = 5
>>>
>>
>> I'm probably dim but I have no idea what that is supposed to mean or do.
>> Is this just calling const(a=5)...? what is the point of that?
>>
>
> I'm not advocating it, and I'm not the one that came up with it. But my
> impression is that it is intended to mean:
>
> a = const('a', 5)
>
> This doesn't seem completely pointless:
>
> >>> class const():
> ... def __init__(self, name, val):
> ... self.name = name
> ... self.val = val
> ... def about(self):
> ... print(self.name, '=', self.val)
> ...
> >>> a = const('a', 5)
> >>> a.val
> 5
> >>> a.about()
> a = 5
>
> There might be a way to subclass, e.g. int, so that you don't need to use
> `a.val` to get the value. It wasn't obvious to me how to do it in pure
> Python with 3 minutes thought.
>
Ah, I get it.
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
---
Ricky.
"I've never met a Kentucky man who wasn't either thinking about going home
or actually going home." - Happy Chandler
_______________________________________________
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/IDIBWXT2OCCLJUGLEQZEAX6SAZLF2SF2/
Code of Conduct: http://python.org/psf/codeofconduct/