How to make a class property?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, the phrase "class property" is ambiguous. Most people actually mean an instance property created with @property. A true property on the class object itself is a different pattern and usually requires a custom descriptor.
The Common Case: Instance Property
This is the standard @property pattern:
This creates a managed attribute on each instance. Access looks like field access, but Python runs the getter and setter methods behind the scenes.
Why @property Is Useful
Properties let you:
- validate assignments
- compute values on demand
- preserve a simple attribute-style API
- change implementation later without changing calling code
That is why @property is so common for encapsulation.
What a True Class-Level Property Means
Sometimes the goal is this:
where some_value is computed like a property on the class itself, not on an instance.
Python's built-in property does not do that out of the box. It is an instance-level descriptor.
Custom Descriptor for Class-Level Access
Here version behaves like a read-only computed property on the class.
Class Property Versus Class Variable
Do not confuse a class property with a plain class variable:
This is just a class attribute. It is perfectly fine when you need shared stored data and no special logic.
You only need a property-like mechanism when access should be computed or controlled.
When to Use Each Pattern
Use an instance property when:
- the value belongs to each object separately
- validation or computed access is needed on instances
Use a class variable when:
- one shared stored value is enough
- no special getter logic is needed
Use a custom class-property descriptor when:
- the class itself should expose a computed attribute
- you want attribute-style access on the class object
Consider @classmethod Too
A lot of "class property" requirements are actually clearer as a class method:
Calling AppConfig.version() is explicit and often easier to understand than introducing a custom descriptor.
Common Pitfalls
The biggest mistake is assuming @property creates a property on the class object. It does not. It creates a descriptor for instance access.
Another mistake is using a custom class-property descriptor when a simple class variable or @classmethod would be clearer.
People also forget that descriptors add complexity. If the value is static and simple, plain class attributes are easier.
Finally, if you validate through @property, store the underlying value under a different name such as _radius to avoid infinite recursion.
One more practical rule helps a lot: if you catch yourself building a writable class-property descriptor, stop and ask whether explicit class methods would be clearer. In many codebases, they are.
Summary
- In Python,
@propertycreates managed attributes on instances, not on the class object itself. - Most "class property" questions are really about ordinary instance properties.
- Use a plain class variable for simple shared data.
- Use a custom descriptor only when you truly need class-level computed attribute access.
- Consider
@classmethodif explicit method syntax is clearer than descriptor magic.
Related reading
- How to make a clean Asynchronous loop?
- How to make a custom activation function with only Python in Tensorflow?
- How to make a datetime object aware not naive?
- How to make a post request with the Python requests library?
- How to make a Python script run like a service or daemon in Linux
- How to make an immutable object in Python?
- How to make good reproducible pandas examples
- How to make lists contain only distinct element in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.