...
/Deciding When to Use Properties
Deciding When to Use Properties
Learn about the roles of attributes, methods, and properties in Python classes, focusing on calculating averages using properties and their applications.
We'll cover the following...
Overview
With the built-in property
blurring the division between behavior and data, it can be confusing to know when to choose an attribute, a method, or a property. In the Color_VP
class in one of our previous lessons, we saw earlier, we added argument value validation
to setting an attribute.
In the NorwegianBlue
class example, in previous lesson, we wrote detailed log entries when attributes were set and deleted. There are also other factors to take into account when deciding to use a property.
In Python, data, properties, and methods are all attributes of a class. The fact that a method is callable does not distinguish it from other types of attributes. We’ll also discover that functions and methods are themselves ordinary objects.
Principles to use properties
The fact that methods are callable attributes, and properties are also attributes, can help us make this decision. We ...