Providing Read-Write Attributes
Providing Read-Write Attributes. You can also use
property() to provide managed attributes with read-write capabilities. In practice, you just need to provide the appropriate getter method (“read”) and setter method (“write”) to your properties in order to create read-write managed attributes.
Let’s say you want your
Circle class to have a
.diameter attribute. However, taking the radius and the diameter in the class initializer seems unnecessary because you can compute one using the other.
The getter method just returns the radius value, which has been stored in the non-public
._radius. There’s a subtle detail to note in this new implementation of
Circle and its
.radius attribute. In this case, the class initializer assigns the input value to the
.radius property directly instead of storing it in a dedicated non-public attribute, such as
Become a Member to join the conversation.