Understanding Python Properties

Reading Time : ~ .

PROPERTIES:

Properties is a class for managing class attributes in Python. Property( ) is a built-in function that creates and returns a property object

 

Syntax:

attribute_name = property(get_Attribute, set_Attribute,del_Attribute(Optional), doc_Attribue(Optional))

where, get_Attritube is function to get value of the attribute,

set_Attribute is function to set value of the attribute,

del_Attribute is function to delete the attribute and

doc_Attribute is a string (like a comment).

Example:

class Celsius:
    def __init__(self, temperature = 0):
        self.temperature = temperature
    def fahrenheit(self):
        return (self.temperature * 1.8) + 32
    def get_temperature(self):
        print("Getting value")
        return self._temperature
    def set_temperature(self, value):
        print("Setting value")
        self._temperature = value
temperature = property(get_temperature,set_temperature)

Here temperature is the property object.property attaches code (get_temperature and set_temperature) to the attribute (temperature).Any code that retrieves the value of temperature will automatically call get_temperature( ).Similarly, any code that assigns a value to temperature will automatically call set_temperature( ).

Example:

>>>c = Celsius( )

Getting value

Here set_temperature( ) was called even when we created an object. The reason is that when an object is created, __init__() method gets called. This method has the line self.temperature = temperature. This assignment called set_temperature( ).

>>>c.temperature
Getting value
0

c.temperature calls get_temperature()

The actual temperature value is stored in the private variable temperature. The attribute temperature is a property object which provides interface to this private variable.

We can use the property with decorators also,Here we are not defining names get_temperature and set_temperature to pollute the class namespace. For this, we reuse the name temperature while defining our getter and setter functions.we can re-write the above example:

class Celsius:
    def __init__(self, temperature = 0):
        self.temperature = temperature
    def fahrenheit(self):
        return (self.temperature * 1.8) + 32
    @property
    def temperature(self):
        print("Getting value")
        return self._temperature
    @temperature.setter
    def temperature(self, value):
        print("Setting value")
        self._temperature = value
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Python Coding Techniques and Programming Practices Anjaneyulu Batta

Coding techniques and programming practices are one of the features of a professional programmer. While writing code to solve a problem programmer should make simple ...

Continue Reading...
Understanding self and __init__ method in python Class. Anjaneyulu Batta

Understand self and __init__ method in python Class?
Before understanding the "self" and "__init__" methods in python class, it's very helpful if we have the ...

Continue Reading...
How to implement Case Insensitive CSV DictReader in python Chaitanya Kattineni

In general use cases we upload the CSV files to the system to store huge amount of data by uploading single file. For example in ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties