Last modified: Nov 27, 2026
Python Class Level Property Guide
A Python class level property lets you control access to class attributes. It looks like a normal attribute. But it runs code behind the scenes.
This guide explains what class level properties are. It shows how to write them. It also covers common mistakes and best practices.
What Is a Property in Python?
A property is a special attribute. It has getter, setter, and deleter methods.
You create one with the property() built-in or the @property decorator. The decorator is the most common style today.
Properties help you add validation and logic. They keep your public interface clean. Many developers wonder when to use property in Python. The short answer is: use it when you need control over reads or writes.
Instance Property vs Class Level Property
Most properties you see are instance properties. They belong to a single object. Each instance has its own value.
A class level property belongs to the class itself. You access it on the class, not on an instance.
This difference matters. Instance properties use self. Class level properties use cls or the class name.
If you are new to self, read what is self in Python explained first. It makes the rest of this article easier to follow.
How to Create a Class Level Property
You have two main options. The first is a metaclass with a property. The second is a class-level descriptor.
Let's start with the simpler idea: a property defined on the class body that reads class data.
class Circle:
pi = 3.14159 # class attribute
@property
def diameter_ratio(self):
# This is an INSTANCE property
return self.pi * 2
c = Circle()
print(c.diameter_ratio)
6.28318
That example is an instance property. It reads a class attribute through self. It is not a true class level property.
To get a property that works on the class itself, you need a metaclass.
Using a Metaclass for Class Level Properties
A metaclass is the class of a class. You can attach a property to the metaclass. Then the property is available on the class object.
class Meta(type):
@property
def name_upper(cls):
# cls is the class, not an instance
return cls.__name__.upper()
class Shape(metaclass=Meta):
pass
print(Shape.name_upper) # works on the class
print(Shape().name_upper) # also works on instances
SHAPE
SHAPE
This works because attribute lookup on an instance falls back to the class. The class lookup then finds the property on the metaclass.
Be careful with cls here. It refers to the class being accessed. This is similar to how self refers to the instance. See Python self parameter in classes for the instance version of this rule.
A Simple Descriptor Alternative
Metaclasses can feel heavy. A descriptor is often easier.
A descriptor is any object with __get__, __set__, or __delete__. Properties are built on descriptors.
class ClassProperty:
def __init__(self, fget):
self.fget = fget
def __get__(self, obj, owner):
# owner is the class
return self.fget(owner)
class Settings:
_debug = False
@ClassProperty
def debug(cls):
return cls._debug
print(Settings.debug)
False
This pattern is short and readable. It works on both the class and its instances.
Adding a Setter to a Class Level Property
You can also allow writes. Just define a setter method on your descriptor.
class ClassProperty:
def __init__(self, fget=None, fset=None):
self.fget = fget
self.fset = fset
def __get__(self, obj, owner):
return self.fget(owner)
def __set__(self, obj, value):
# obj is None when setting on the class
self.fset(type(obj) if obj else obj, value)
def setter(self, fset):
return ClassProperty(self.fget, fset)
class Config:
_level = 1
@ClassProperty
def level(cls):
return cls._level
@level.setter
def level(cls, value):
if not isinstance(value, int):
raise TypeError("level must be an int")
cls._level = value
Config.level = 3
print(Config.level)
3
Now the class validates its own data. That is the main reason to use a property instead of a plain attribute.
If you want to pass extra arguments to a property, read Python property with arguments. It covers a related pattern.
Class Level Property vs Plain Class Attribute
A plain class attribute is simple. But it offers no control.
Anyone can overwrite it. There is no validation. There is no computed logic.
A class level property gives you both. You can compute values on the fly. You can reject bad input.
Use a plain attribute for constants. Use a property when the value must be checked or derived.
Common Mistakes
Many beginners write a property inside a class. Then they try to call it on the class. This fails.
class User:
@property
def role(self):
return "admin"
# This raises an error
print(User.role)
The output is a property object, not the value. That surprises people. The property only runs when accessed on an instance.
Another mistake is forgetting cls. Inside a class level property, use cls, not self. Mixing them causes confusing errors.
The __init__ method also trips people up. It runs per instance. It does not run for class level data. See Python __init__ method explained for details.
When Should You Use One?
Use a class level property when the data is shared. Examples include configuration flags, registry counters, and computed constants.
Do not use one for per-object data. That is what normal properties are for.
Keep the logic small. A property should be fast. It should not do heavy work or hit the network.
Keep it readable. If a property needs a long comment, consider a method instead.
Testing Your Property
Always test both read and write paths. Check the error case too.
try:
Config.level = "high"
except TypeError as e:
print(e) # level must be an int
level must be an int
These tests catch the bugs beginners make most often.
Conclusion
Python class level properties give you control over class data. You can build them with a metaclass or a small descriptor.
They add validation and computed logic. They keep your class interface clean and safe.
Start with a simple descriptor. Move to a metaclass only if you need it. Test your getters and setters. Then your class level properties will be reliable and easy to read.