Search Here

What makes the Python Property() function and Decorator so amazing?

What makes the Python Property() function and Decorator so amazing?

In this post, we’ll be learning amazing things about Python Property() function and Decorator and dig deeper into a getter, setter, and deleter in python.

The Python property() method is useful in associating attributes belonging to the specific object.
The object-Oriented Programming property() method makes it simpler for assigning values and retrieving values associated with the object.

Table of Contents

Starting with a simple example

First, we will see how our code looks/works without using the property() method as it helps us to understand the advantages it provides.
Let us start by creating a class Client and specify values such as first_name,last_name,company.

class Client():
    def __init__(self,first_name=None,last_name=None,company=None):
        self.first_name=first_name
        self.last_name=last_name
        self.full_name="{} {}".format(self.first_name,self.last_name)
        self.company=company

    def get_client_company_name(self):
        return self.company

# created new client object and assigned values such as first_name,last_name,company,email,designation.
client_one = Client(first_name="Rahul",last_name="Malhotra",company="Relience Corporation LTD")

# calling function get_client_fullname() to retrive the full name of client  
print('Client full name : {} \n'.format(client_one.full_name))
print('Client company : {} \n'.format(client_one.get_client_company_name()))

PYTHON OUTPUT
Client full name : Rahul Malhotra 

Client company : Relience Corporation LTD 

We are calling method to get the client details from the object.

Let us say we have updated the variable first_name value before printing the variable full_name.

client_one.first_name="Vikram"
print('Client full name : {} \n'.format(client_one.full_name))
print('Client company : {} \n'.format(client_one.get_client_company_name()))

PYTHON OUTPUT
Client full name : Rahul Malhotra # this first_name has not changed

Client company : Relience Corporation LTD 

Has we can see after updating first_name value the updated name is not displayed on the screen to display we need to call function and that function returns the updated value.
In real-time projects, this kind of issue will cost in lines, development, and testing time. Now we understood the problems we face without using the property() method.

Using property() method

We can recreate the above same example using the property() method.

Syntax

property(fget,fset,fdel,doc)

The Property function arguments.

  • fget : takes function has argument used for getting values using attribute
  • fset : takes function has argument used for setting values using attribute
  • fdel : takes function has argument used for deleting values using attribute
  • doc : takes function has argument used for describing the attribute
class Client():
    def __init__(self,first_name=None,last_name=None,company=None):
        self.first_name=first_name
        self.last_name=last_name
        self.full_name="{} {}".format(self.first_name,self.last_name)
        self.company=company

    def get_full_name(self):
        self.full_name="{} {}".format(self.first_name,self.last_name)
        return self.full_name

    def set_first_name(self,value):
        self.first_name=value

    full_name_attr = property(get_full_name,set_first_name)

    def get_client_company_name(self):
        return self.company

# created new client object and assigned values such as first_name,last_name,company,email,designation.
client_one = Client(first_name="Rahul",last_name="Malhotra",company="Relience Corporation LTD")

# calling function get_client_fullname() to retrive the full name of client  
client_one.first_name="Vikram"
print('Client full name : {} \n'.format(client_one.full_name_attr))
print('Client company : {} \n'.format(client_one.get_client_company_name()))

PYTHON OUTPUT
Client full name : Vikram Malhotra 

Client company : Relience Corporation LTD 

The Attribute full_name_attr functions as a single entity were we can call value assign and delete.
There are other ways for assigning property such as the decorator method. These methods are very elegant and clean.

Moving to various ways of declaring property()

Let look at other example and create a property using a decorator.

    class Product:
        def __init__(self):
            self.name=None

        @property
        def get_name(self):
            return self.name
        
        @get_name.setter
        def get_name(self,value):
            self.name=value

        @get_name.deleter
        def get_name(self):
            del self.name

    p1 = Product()
    p1.get_name="Hand Bag"
    print("Product Name : {}\n".format(p1.get_name))
    del p1.get_name

    PYTHON OUTPUT
    Product Name : Hand Bag

As we can observe the above example get_name is a property and by the same name we are specifying other method and adding @get_name.setter,@get_name.deleter attributes.
The p1.get_name="Hand Bag" will call setter and sets the value of name and p1.get_name this will call the getter method which returns the value of name and calling del p1.get_name with del keyword will call deleter method.

Conclusion

So we have reached the end of our post on What makes Python Property() function and Decorator so amazing?. Till now we have covered the importance of property, different ways we can set property and property operations such as getter, setter, and deleter. If you have any doubts kindly comment and you’ll surely hear from us and share if you like.

Related Posts

Summary
Review Date
Reviewed Item
What makes the Python Property() function and Decorator so amazing?
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Langauage