This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add classproperty as builtin class
Type: enhancement Stage: test needed
Components: Interpreter Core Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, eric.snow, erik.bray, guettli, ryanhiebert, yselivanov
Priority: normal Keywords:

Created on 2015-08-26 07:07 by guettli, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg249182 - (view) Author: Thomas Guettler (guettli) * Date: 2015-08-26 07:07
Quoting Guido van Rossum Aug 20 2015. Thread "Properties for classes possible?"

https://mail.python.org/pipermail/python-ideas/2015-August/035354.html

{{{
 think it's reasonable to propose @classproperty as a patch to CPython. It
needs to be C code. Not sure about the writable version. The lazy=True part
is not appropriate for th he stdlib (it's just a memoize pattern).
}}}

The solution I use at the moment:

{{{
# From http://stackoverflow.com/a/5192374/633961
class classproperty(object):
    def __init__(self, f):
        self.f = f
    def __get__(self, obj, owner):
        return self.f(owner)
}}}

According to Terry Jan Reedy the next step is to find
someone to translate this to C.

Sorry, my C knowledge is limited. Can anybody help?
msg249210 - (view) Author: Erik Bray (erik.bray) * (Python triager) Date: 2015-08-26 17:16
A read-only classproperty is fairly trivial to implement in pure Python and has been done.

A good reason to have a classproperty implementation in CPython would be to support settable/deleteable classproperties.  The problem is that can't be done with the descriptor protocol on its own without a custom metaclass.  classproperty would have to somehow be special-cased, perhaps in type_setattro, say.  I don't know if that can just be done lightly.

Otherwise I don't see the advantage of adding a classproperty type to CPython?
msg249339 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-08-29 21:40
I've posted a counter-proposal on python-ideas:

https://mail.python.org/pipermail/python-ideas/2015-August/035614.html

Basically: instead of "classproperty", add a more lenient alternative to classmethod which allows composition.  I called it "classresolved".

class classresolved:
    def __init__(self, wrapped):
        self.wrapped = wrapped
    def __get__(self, obj, cls):
        try:
            getter = self.wrapped.__get__
        except AttributeError:
            return self.wrapped
        return getter(cls, type(cls))
msg249340 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-08-29 21:43
I also posted a broader proposal:

https://mail.python.org/pipermail/python-ideas/2015-August/035615.html
History
Date User Action Args
2022-04-11 14:58:20adminsetgithub: 69129
2015-08-29 21:43:05eric.snowsetmessages: + msg249340
2015-08-29 21:40:45eric.snowsetnosy: + eric.snow
messages: + msg249339
2015-08-26 17:16:18erik.braysetnosy: + erik.bray
messages: + msg249210
2015-08-26 15:38:29ryanhiebertsetnosy: + ryanhiebert
2015-08-26 15:38:17terry.reedysettitle: classproperty -> Add classproperty as builtin class
stage: needs patch -> test needed
2015-08-26 14:30:44yselivanovsetversions: + Python 3.6
nosy: + yselivanov

components: + Interpreter Core
type: enhancement
stage: needs patch
2015-08-26 13:38:46barrysetnosy: + barry
2015-08-26 07:07:03guettlicreate