Python @property decorator

The @property is used to define new properties or modify existing ones. For example:

class CodesCracker:
    def __init__(self, val):
        self.v = val

    @property
    def val(self):
        print("\nGetting the Value")
        return self.v

    @val.setter
    def val(self, val):
        print("\nNow Setting the Value to \"", val, "\"", sep="")
        self.v = val

    @val.deleter
    def val(self):
        print("\nDeleting the Value")
        del self.v

a = CodesCracker("Python is Fun!")
print(a.val)

a.val = "Python is an Object-oriented PL"
print(a.val)

del a.val

The sample output of the above program, demonstrating the @property decorator in Python, is shown in the snapshot given below:

python property decorator

The way to use the @property decorator, either to define new properties or to modify the existing ones, is:

class C(object):
    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

For example:

class CodesCracker:
    def __init__(self, stud):
        self.s = stud

    @property
    def stud(self):
        print("\nGetting the Name of Student...")
        return self.s

    @stud.setter
    def stud(self, nstud):
        print("\nNow Setting Name of Student to \"", nstud, "\"", sep="")
        self.s = nstud

    @stud.deleter
    def stud(self):
        print("\nDeleting the Name of Student...")
        del self.s


x = CodesCracker("Emily Cale")
print("The Name of Student is:", x.stud)
x.stud = "Joey Lynn King"
print("Now the Name of Student is:", x.stud)
del x.stud

The snapshot given below shows, of course, the sample output of this program:

python property decorator example

Advantages of the @property decorator in Python

  • Encapsulation: @property enables developers to conceal the implementation details of a class while providing a straightforward interface for accessing properties. This promotes encapsulation, a fundamental principle of object-oriented programming that facilitates the organization and maintenance of code.
  • @property can be used to create computed properties derived from other class attributes or methods. This is useful when calculating a value based on other attributes, such as converting Celsius to Fahrenheit.
  • Using @property can simplify code by removing the requirement for explicit getter methods. This makes the code more readable and concise.
  • @property prevents modification by making an attribute read-only. Thus, it prevents unintended modification of the attribute, thereby enhancing the safety of the code.

Disadvantages of the @property decorator in Python

  • Since @property executes a method to calculate the property value, it can result in a performance overhead compared to direct attribute access. For heavily utilized classes, this performance overhead can become significant.
  • The @property decorator can be confusing for Python beginners, particularly if they are unfamiliar with computed properties. This may result in bugs or subpar code.
  • Issues with Compatibility: Some third-party libraries or tools may not function correctly with @property, particularly if they rely on direct attribute access. This may necessitate additional workarounds to resolve compatibility problems.

Python Online Test


« Previous Tutorial Python Keywords »



Liked this post? Share it!