By way of correcting misconceptions:
On 12/18/21 8:39 AM, Chris Angelico wrote:
>
> I'm not sure that this is actually possible the way you're doing it.
> The descriptor protocol (which is what makes properties work) won't
> apply when you're looking up statically.
On 12/18/21 9:19 AM, Christopher Barker wrote:
>
> Anyway, the thing is that both staticmethod and property are implimented
using descriptors, which I think can only be
> invoked by instance attribute lookup. That is, the class attribute IS a
descriptor instance.
While it is true that a descriptor does not get the chance to run its `__set__` and `__delete__` methods when called on
the class, it does get to run its `__get__` method. In code:
class descriptor_example:
#
def __get__(self, instance, owner=None):
if owner is not None:
print('called directly from the class, have a cookie!')
return 'chocolate-chip cookie'
#
def __set__(self, instance, value):
# never called from the class
raise TypeError
#
def __delete__(self, instance, value):
# never called from the class
raise TypeError
class Test:
huh = descriptor_example()
>>> Test.huh
called directly from the class, have a cookie!
chocolate-chip cookie
>>> Test.huh = 7 # change `huh`
>>> Test.huh
7
>>> Test.huh = descriptor_example() # put it back
>>> Test.huh
called directly from the class, have a cookie!
chocolate-chip cookie
>>> del Test.huh # remove huh
>>> Test.huh
AttributeError
Having said that, I don't think it's needs direct support in the stdlib.
--
~Ethan~
_______________________________________________
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/CEJJBFG27RI3DQ6H5XJB6TBYGF57BONC/
Code of Conduct: http://python.org/psf/codeofconduct/