Skip to main content

Class property that shares the same value with the class and any instance.

Project description


pip install class_property


  • class_value - Hold a single value for a class and all instances.
  • class_property - Like a property, but works on MyClass.class_prop = value
  • metaclass - Create a metaclass that looks for class_value and class_property to register them.
  • decorate - Decorate a class to register any class_value and class_property.

A class must use the metaclass or decorate functions. Subclasses do not need to use decorate or metaclass. The metaclass is how the class object works with the descriptor.


Class value usage.


from class_property import class_value

class MyClass(object):
    value = class_value(1)

mc = MyClass()
assert mc.value == 1
assert MyClass.value == 1

MyClass.value = 3
assert mc.value == 3
assert MyClass.value == 3

mc.value = 2
assert mc.value == 2
assert MyClass.value == 2

class SubClass(MyClass):
    hello = class_value("World")

sub = SubClass()
SubClass.hello = 'name'
assert sub.hello == 'name'
assert SubClass.hello == 'name'

sub.hello = 'John Doe'
assert sub.hello == 'John Doe'
assert SubClass.hello == 'John Doe'

sub.value = 7
assert SubClass.value == 7
assert sub.value == 7
assert mc.value == 7
assert MyClass.value == 7

Class property usage.


from class_property import class_value, class_property, decorate, metaclass

global GLOB
GLOB = 'Hello'

def get_glob():
    """Return the global GLOB value"""
    global GLOB
    return GLOB

def set_glob(value):
    global GLOB
    GLOB = value

# doesn't matter if class_value.decorate, class_property.decorate, or decorate (same with metaclass)
class MyClass(object, metaclass=metaclass()):
    _VALUE = None

    def value(self):
        return MyClass._VALUE

    def value(self, value):
        MyClass._VALUE = value

    # Also works with no arguments
    def value2():
        return MyClass._VALUE

    def value2(value):
        MyClass._VALUE = value

    glob = class_property(get_glob, set_glob)

mc = MyClass()
assert mc.value is None
assert MyClass.value is None
MyClass.value = 3
assert mc.value == 3
assert MyClass.value == 3
mc.value = 2
assert mc.value == 2
assert MyClass.value == 2

assert mc.value2 == 2
assert MyClass.value2 == 2
mc.value2 = 5
assert mc.value == 5
assert MyClass.value == 5
assert mc.value2 == 5
assert MyClass.value2 == 5

assert MyClass.glob == 'Hello'
assert mc.glob == 'Hello'
MyClass.glob = 'Jack'
assert MyClass.glob == 'Jack'
assert mc.glob == 'Jack'
mc.glob = 'Jill'
assert MyClass.glob == 'Jill'
assert mc.glob == 'Jill'

class SubClass(MyClass):

sub = SubClass()
sub.glob = 'John'
assert SubClass.glob == 'John'
assert sub.glob == 'John'
assert MyClass.glob == 'John'
assert mc.glob == 'John'

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for class-property, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size class_property-0.0.1.tar.gz (5.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page