Properties in Detail
Learn about the property function in detail.
We'll cover the following
The property()
function
Think of the property
function as returning an object that proxies any requests to get or set the attribute value through the method names we have specified. The property()
built-in is like a constructor for such an object, and that object is set as the public-facing member for the given attribute.
This property()
constructor can actually accept two additional arguments,
a delete
function and a docstring for the property. The delete
function is rarely supplied in practice, but it can be useful for logging the fact that a value has been deleted, or possibly to veto deleting if we have reason to do so. If we do not supply this parameter, the docstring will instead be copied from the docstring for the first argument: the _get_state()
method.
Example
Here is a silly example that states whenever any of the methods are called:
Get hands-on with 1200+ tech skills courses.