Note: In Python 2, make sure that your class inherits from object (making it a new-style class) in order for all features of properties to be available.
Overriding just a getter, setter or a deleter of a property object
When you inherit from a class with a property, you can provide a new implementation for one or more of the property getter, setter or deleter functions, by referencing the property object on the parent class:
You can also add a setter or deleter where there was not one on the base class before.
Using properties without decorators
While using decorator syntax (with the @) is convenient, it also a bit concealing. You can use properties directly, without decorators. The following Python 3.x example shows this:
Using the @property decorator
The @property decorator can be used to define methods in a class which act like attributes. One example where this can be useful is when exposing information which may require an initial (expensive) lookup and simple retrieval thereafter.
Given some module foobar.py:
Using the @property decorator for read-write properties
If you want to use @property to implement custom behavior for setting and getting, use this pattern:
To use this:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0