aboutsummaryrefslogtreecommitdiff
blob: b328901d78c33fa9b7d33a778e341cb73adc02ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Classes implementing the descriptor protocol."""

__all__ = ("classproperty",)


class classproperty(object):

    """Like the builtin :py:func:`property` but takes a single classmethod.

    Essentially, it allows you to use a property on a class itself- not
    just on its instances.

    Used like this:

    >>> from snakeoil.descriptors import classproperty
    >>> class foo(object):
    ...
    ...   @classproperty
    ...   def test(cls):
    ...     print("invoked")
    ...     return True
    >>> foo.test
    invoked
    True
    >>> foo().test
    invoked
    True
    """

    def __init__(self, getter):
        self.getter = getter

    def __get__(self, instance, owner):
        return self.getter(owner)