classproperties

raw JSON →
0.2.0 verified Fri May 01 auth: no python maintenance

A lightweight Python library providing property decorators for classmethods and staticmethods. Version 0.2.0 is the latest release. The project is minimally maintained.

pip install classproperties
error TypeError: @classproperty can only be used on methods with 'cls' as first argument
cause The decorated method does not accept 'cls' as its first parameter, or the decorator is applied to a non-method.
fix
Make sure the method signature is like: def method(cls): and that @classproperty is applied inside a class body.
error AttributeError: 'function' object has no attribute '__get__'
cause Using an incompatible Python version or applying the decorator incorrectly (e.g., on a non-function). The library may not support Python 2.
fix
Ensure Python 3. Use only on methods defined inside a class.
gotcha The decorator must be used inside a class definition and the method must accept 'cls' as its first argument. Defining it outside a class will raise an error.
fix Ensure the decorated function is a classmethod-like method with 'cls' parameter inside a class.
gotcha Inheritance may not work as expected if subclasses override the property without using the decorator. The parent class property will be used if overridden as a regular attribute.
fix Always apply @classproperty explicitly in subclasses that need a different value.

Basic usage of classproperty decorator.

from classproperties import classproperty

class MyClass:
    @classproperty
    def my_prop(cls):
        return 'classprop_val'

print(MyClass.my_prop)