Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Difference between attributes and properties in python?
In Python, everything is an object, and every object has attributes and methods. Attributes are data variables that describe the object (such as name, age, or height), while properties are a special kind of attribute that use getter, setter, and deleter methods to control access to the underlying data.
Attributes in Python
Attributes are variables that belong to an object. They are usually defined in the __init__ method and accessed using dot notation ?
Example
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
my_dog = Dog("Fido", 3)
print(my_dog.name)
print(my_dog.age)
The output of the above code is ?
Fido 3
Here, name and age are plain attributes − they store data directly and can be read or written without any additional logic.
Properties in Python
Properties are methods that behave like attributes. They are defined using the @property decorator and let you run custom logic (such as calculations or validation) when a value is accessed, set, or deleted ?
Example: Read-Only Property
class Square:
def __init__(self, side):
self.side = side
@property
def area(self):
return self.side ** 2
my_square = Square(5)
print(my_square.area)
The output of the above code is ?
25
Here, area is not stored as data. It is a property that calculates a value from the side attribute each time it is accessed.
Example: Property with Getter, Setter, and Deleter
The @property decorator can also define setter and deleter methods for full control over an attribute ?
class C:
def __init__(self, x):
self._x = x
@property
def x(self):
print("Getting x")
return self._x
@x.setter
def x(self, value):
print("Setting value to " + value)
self._x = value
@x.deleter
def x(self):
print("Deleting x")
del self._x
y = C("Happy Holidays")
print(y.x)
y.x = "Merry Christmas!"
del y.x
The output of the above code is ?
Getting x Happy Holidays Setting value to Merry Christmas! Deleting x
Attributes vs Properties in Python
The key difference is that attributes simply store data, while properties execute logic when accessed. The following example demonstrates both side by side ?
Example
class BankAccount:
def __init__(self, balance, interest_rate):
self.balance = balance
self.interest_rate = interest_rate
@property
def interest_earned(self):
return self.balance * self.interest_rate
acct1 = BankAccount(1000, 0.05)
print("Balance (attribute):", acct1.balance)
print("Interest earned (property):", acct1.interest_earned)
The output of the above code is ?
Balance (attribute): 1000 Interest earned (property): 50.0
Here, balance and interest_rate are attributes that store data directly. interest_earned is a property that computes a value from those attributes each time it is accessed.
Example: Rectangle with Attributes and Properties
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
@property
def area(self):
return self.width * self.height
@property
def perimeter(self):
return 2 * (self.width + self.height)
rect = Rectangle(5, 3)
print("Width:", rect.width)
print("Height:", rect.height)
print("Area:", rect.area)
print("Perimeter:", rect.perimeter)
The output of the above code is ?
Width: 5 Height: 3 Area: 15 Perimeter: 16
Conclusion
Attributes are plain data members that store values directly, while properties are methods decorated with @property that look like attributes but execute custom logic (calculations, validation, or side effects) when accessed, set, or deleted.
