Python Reference ยท Built-in Functions
Python property() function
Reference for Python property() and the @property decorator: getters, setters, deleters, read-only and computed attributes, validation and common pitfalls.
property() creates a managed attribute: reading, assigning or deleting obj.name calls a method you wrote, while the calling code still looks like plain attribute access. It is how Python classes add validation or computed values later without changing their public interface.
You almost always use it as a decorator. @property turns a method into a read-only attribute; then @name.setter and @name.deleter add assignment and deletion behavior.
class Circle: def __init__(self, radius): self.radius = radius @property def area(self): return 3.14159 * self.radius ** 2
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
@property def name(self): ... @name.setter def name(self, value): ... @name.deleter def name(self): ... name = property(fget=None, fset=None, fdel=None, doc=None)
| Name | Description |
|---|---|
fget |
Function called to read the attribute. Receives the instance. |
fset |
Function called on assignment. Receives the instance and the new value. |
fdel |
Function called by del obj.name. |
doc |
Docstring. If omitted, the docstring of fget is used. |
Return value: a property object (a data descriptor) to store in the class body.
Raises: AttributeError when you read, set or delete without the matching function ("property has no setter", "can't delete attribute", ...).
Usage notes
- Properties live on the class, not the instance, and only work on classes inheriting
object(all classes in Python 3). - Store the real value under another name, conventionally with a leading underscore (
_celsius). Using the same name as the property causes infinite recursion. - Setters run on every assignment, including inside
__init__. Assigningself.celsius = celsiusthere validates the initial value too. - Read-only attributes: define only the getter. Assigning then raises
AttributeError. - Computed on every access. A property recalculates each time. For an expensive value computed once, use
functools.cached_property. - Subclasses can override a property, but if you replace only the getter, the setter is lost unless you reuse
Parent.name.setter. - Keep them cheap and side-effect free. Attribute access that does slow I/O surprises callers; make that a method.
- Start simple. Python does not need
get_x()/set_x()pairs from day one; use plain attributes and add a property when you need logic. - Class-level access returns the
propertyobject itself (Account.nickname), which is handy for introspection and docs.