On this page
property
Prev: print | Up: __builtins__ | Next: quit
Rel: classmethod, staticmethod, super
Ref: docs.python.org — property
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import property
Overview & Signature
property(fget=None, fset=None, fdel=None, doc=None)
Property attribute.
Common Methods & Attributes
property.deleter(self, object, /)— Descriptor to obtain a copy of the property with a different deleter.property.fdel(...)—property.fdelmethod or attribute.property.fget(...)—property.fgetmethod or attribute.property.fset(...)—property.fsetmethod or attribute.property.getter(self, object, /)— Descriptor to obtain a copy of the property with a different getter.property.setter(self, object, /)— Descriptor to obtain a copy of the property with a different setter.
Usage
class Circle:
def __init__(self, r: float) -> None:
self._r = r
@property
def radius(self) -> float:
return self._r
@radius.setter
def radius(self, val: float) -> None:
if val < 0:
raise ValueError("radius must be >= 0")
self._r = val
c = Circle(2.0)
c.radius = 5.0
assert c.radius == 5.0
Gotchas
- Read-only until you add
@prop.setter: Assigning to a getter-only property raisesAttributeError. - Use
functools.cached_propertywhen computing the value is expensive and immutable.
REPL Inspect
help(property)
Linked from
- tuts/python3/__builtins__
- tuts/python3/__builtins__/__builtins__
- tuts/python3/__builtins__/classmethod
- tuts/python3/__builtins__/print
- tuts/python3/__builtins__/quit
- tuts/python3/__builtins__/staticmethod
- tuts/python3/__builtins__/super
- tuts/python3/python decorators
- tuts/python3/tutorials/advanced_python_internals/decorators_and_closures
- tuts/python3/tutorials/intermediate_python_and_software_engineering/object_oriented_programming
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/classmethod Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/print Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/quit Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/staticmethod Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/super Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/python decorators links to this page
- tuts/python3/tutorials/advanced_python_internals/decorators_and_closures links to this page
- tuts/python3/tutorials/intermediate_python_and_software_engineering/object_oriented_programming links to this page