Sitemap
Vubon Roy

Vubon Roy

Speak Math, Not code.

Property a built-in Python Function

3 min readSep 14, 2019
Press enter or click to view image in full size

Hmm. I am thinking what is that? let’s deep a dive with me. Before I start I want to ask some questions myself.
1. What is property in Python?
2. When should I use the Property function?
3. How to use the property function?
In this article, you will understand what exactly the Python Property does.

  1. What is the Property in Python?
    In Python property is a built-in function. But you can also use the property as a decorator. Property function is helping to modify class attributes and give backward compatibility. But you should have a basic concept of Python classes and method. Because of the property is typically used inside one.
  2. When should I use the Property function?
    Assume, You create a class that store user bank account information like a bank account number and balance.
    Let’s create the class

Now, from this account we transfer the fund what will we get after calling the account_information attribute? Let’s transfer the balance and call the account_information attribute of the instance.

No change! But we transferred the balance. We expected that the balance should be 500.25. But it does not happen. Because Python does not change the account_information value. If we call the balance attribute we will get the exact balance. I mean the Current balance is 500.25.

Get Vubon Roy’s stories in your inbox

Join Medium for free to get updates from this writer.

Assume, you are working with a team and you are the owner of this bank account module. Also, you share your bank account module with your colleagues. Let’s your team members are working with a new module that is called Payment Gateway and they want to charge an amount from the bank account. When they call the account_information attribute. They will get the account balance is 1000.50. But we had already transferred 500.25 amount from that account. In this case, all team members are falling in trouble.

In this case, we can solve the problem by creating a new method of account class. Let’s create the method.

Now we get the expected result. But Did you notice?? I commented out the account_information attribute and created a new method as the same name as the attribute. It has a backward compatibility issue. Because your teammates do not know the new method as the same name as the attribute and their codebase lines could be 1k or more than of their Payment Gateway module.

In this case, The Property will help to solve these two types of issue. The balance amount change problem and backward compatibility.

3. How to use the property function?
Let’s solve the problem in a Pythonic way.

Both problems solved by using just property decorator. But The property function has more features. Such as you can set a new value, get the value, and delete the value.
Example:

Hope the purpose of Property is clear and you now know when and how to use it. If you did, congratulations! I will meet you in the next one.

--

--