Welcome! In this article, you will learn how to work with the You will learn: Let's start with a little bit of context. Why would you use properties in Python? Properties can be considered the "Pythonic" way of working with attributes because: These advantages make properties a really awesome tool to help you write more concise and readable code. ? A decorator function is basically a function that adds new functionality to a function that is passed as argument. Using a decorator function is like adding chocolate sprinkles to an ice cream ?. It lets us add new functionality to
an existing function without modifying it. In the example below, you can see what a typical decorator function looks like in Python: Let's analyze these elements in detail: If we run the code, we see this output:
Notice how the decorator function runs even if we are only
calling 💡Note: In general, we would write I know you may be asking: how is this related to the @property? The @property is a built-in decorator for the property() function in Python. It is used to give "special" functionality to certain methods to make them act as getters, setters, or deleters when we define properties in a class. Now that you are familiar with decorators, let's see a real scenario of the use of @property! 🔸 Real-World Scenario: @propertyLet's say that this class is part of your program. You are modeling a house with a
This instance attribute is public because its name doesn't have a leading underscore. Since the attribute is currently public, it is very likely that you and other developers in your team accessed and modified the attribute directly in other parts of the program using dot notation, like this:
💡 Tip: obj represents a variable that references an instance of So far everything is working great, right? But let's say that you are asked to make this attribute protected (non-public) and validate the new value before assigning it. Specifically, you need to check if the value is a positive float. How would you do that? Let's see. Changing your CodeAt this point, if you decide to add getters and setters, you and your team will probably panic ?. This is because each line of code that accesses or modifies the value of the attribute will have to be modified to call the getter or setter, respectively. Otherwise, the code will break ⚠️.
But... Properties come to the rescue! With Awesome, right? If you decide to use
Specifically, you can define three methods for a property:
Price is now "Protected"
In Python, by convention, when you add a leading underscore to a name, you are telling other developers that it should not be accessed or modified directly outside of the class. It should only be accessed through intermediaries (getters and setters) if they are available. 🔸 GetterHere we have the getter method:
Notice the syntax:
Here is an example of the use of the getter method:
Notice how we access the price attribute as if it were a public attribute. We are not changing the syntax at all, but we are actually using the getter as an intermediary to avoid accessing the data directly. 🔹 SetterNow we have the setter method:
Notice the syntax:
This is an example of the use of the setter method with @property:
Notice how we are not changing the syntax, but now we are using an intermediary (the setter) to validate the argument before assigning it. The new value (45000.0) is passed as an argument to the setter :
If we try to assign an invalid value, we see the descriptive message. We can also check that the value was not updated:
💡 Tip: This proves that the setter method is working as an intermediary. It is being called "behind the scenes" when we try to update the value, so the descriptive message is displayed when the value is not valid. 🔸 DeleterFinally, we have the deleter method:
Notice the syntax:
💡 Tip: Notice that the name of the property is "reused" for all three methods. This is an example of the use of the deleter method with @property:
The instance attribute was deleted successfully ?. When we try to access it again, an error is thrown because the attribute doesn't exist anymore. 🔹 Some final TipsYou don't necessarily have to define all three methods for every property. You can define read-only properties by only including a getter method. You could also choose to define a getter and setter without a deleter. If you think that an attribute should only be set when the instance is created or that it should only be modified internally within the class, you can omit the setter. You can choose which methods to include depending on the context that you are working with. 🔸 In Summary
I really hope you liked my article and found it helpful. To learn more about Properties and Object Oriented Programming in Python, check out my online course, which includes 6+ hours of video lectures, coding exercises, and mini projects. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What does a property decorator do?The @property is a built-in decorator for the property() function in Python. It is used to give "special" functionality to certain methods to make them act as getters, setters, or deleters when we define properties in a class.
What is property () in Python?The property() method in Python provides an interface to instance attributes. It encapsulates instance attributes and provides a property, same as Java and C#. The property() method takes the get, set and delete methods as arguments and returns an object of the property class.
How is Python property decorator implemented?The property decorator is implemented with a pattern similar to the my_decorator function. Using the Python @decorator syntax, it receives the decorated function as an argument, just like in my example: some_func_decorated = my_decorator(some_func) . Note that I changed some function names for clarity.
What is decorator module in Python?In this tutorial on decorators, we'll look at what they are and how to create and use them. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
|