python @property decorator

How to use python property decorator?

In this lesson, you’ll learn about the Python property decorator, which is a pythonic approach to using getters and setters in object-oriented programming.

The @property decorator is a built-in decorator in Python that allows you to define properties without having to call the inbuilt function property().

So it returns a class’s property attributes from the getter, setter, and deleter parameters.

What Is The Difference Between Getters And Setters?

Getters: In Object-Oriented Programming (OOP), these are the methods that allow you to access a class’s private attributes.

Setters: These are the methods that are useful in the object-oriented programming feature to set the value of private attributes in a class.

python property decorator

Here is a simple example of setting and getting properties in python.

# python property decorator example
class ClassName:
    def __init__(self, val):
        self.__name = val

    # getter method to get the properties using an object
    def get_name(self):
        return self.__name

    # setter method to set the value 'name' using an object
    def set_name(self, val):
        self.__name = val

There are three methods in the above ClassName (example).

  1. __init__ Initialize a class’s attributes or properties.
    • __name:- It is a private attribute.
  2. get_name: This function retrieves the values of the private attribute name.
  3. set_name: Set the value of the name variable using a class object.

In Python, you can’t directly access the private variables. That’s why you included the getter method in your code.

Here is how the above example is interactive

# python property decorator example
# creating an object
obj = ClassName("Jhon")

# getting the value of 'name' using get_name() method

# setting a new value to the 'name' using set_name() method

This is how you can implement the private attributes, getters, and setters in Python.
Let’s look at how to use the @property decorator to implement the above class.

# python property decorator example
class ClassName:

    def __init__(self, val): = val

    def name(self):
        return self.__name

    def name(self, val):
        self.__name = val

We use the @property to get the value of a private attribute without using any getter methods

In front of the method where we return the private variable, we must add a decorator @property.

We use the @method_name.setter in front of the method to set the value of the private variable. It must be used as a setter.

How to hide the setter and getter methods and make them private?

Hide the setter and getter methods and make them private is quite simple in python.
Here is how you can do it.

# python property decorator example
    def __get_name(self):
        return self.__name

    def __set_name(self, val):
       self.__name = val

Youtube – The python property decorator

You’ve learned how to use Python to construct encapsulation and the differences between setter, getter, and property.