Python Properties — simplified!
Introduction
In Python, we know that there is no such thing as a private attribute. As such an attribute value could be set to a value that may not be logical for that attribute to have. For example, a name attribute as number, negative height, blank age etc. In this article we will discuss and explore a way in which we can encapsulate attributes so that we can intercept what values they are being assigned to, validate them and raise error they doesn’t seem logical.
We will see how we can make use of something called a property object to accomplish this. We won’t go into the most commonly used @property decorator yet, but will try to understand what actually happens under the hood of property.
Let’s get started.
A simple way to create and initialize a Employee class with a name attribute would be this -
By convention, if we want to let other developers know that the name attribute is intended to be used as private attribute we will rename it to _name. But this will in no way can prevent others to access it outside the Employee class.
Though there is no harm in keeping this attribute accessible from outside the class, there is no way to control what value can be set to _name. It could be set to whatever one pleases to.
This is not correct, right? So, we need to find a way in which we can control that only something that could be a name can be assign to an employees name attribute. For our purpose and simplicity we will consider any non-zero length string as a valid name.
Get Sapan Roy’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s see how we can re-write the Employee class. We will define a setter and getter methods to set and get Employee name.
If we now try to set an employee’s name to an invalid value we can raise an exception.
Re-writing the Employee class this way provides us a way to control that all employees will have a valid name. But setting and getting an employee name using set_name() and get_name() method certainly takes away the convenience of accessing their name using dot notation like emp1.name.
Can we have a way to access name as emp1.name but still having the control that only a valid name can be assigned to every employee. Let’s see how we can achieve this. We will use a very special class in Python called property class.
Here we have created name as a property object in the Employee class. A property class takes in the getter and setter method, besides other, and creates a property object.
When we try to access this name property get_name() and set_name() methods connected to it will be automatically called.
As we can see above, when we try to set the value set_name() method gets executed and when we try to access it get_name() gets executed.
What if we try to set the employee name to nothing? Sure, try it out! The name property method will still intervene, execute the set_name() method and ensure that that doesn’t happens.
You see. Now even though an employee’s name is accessible from outside the class, we can intercept the value, validate it and be sure that it will be always be set to a valid name!






