
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Attributes and Properties in Python
In Python, everything is an object. And every object has attributes and methods, or functions. Attributes are described by data variables, for example like name, age, height, etc.
Properties
Properties are a special kind of attributes that have getter, setter, and delete methods like __get__, __set__, and __delete__ methods.
A property decorator in Python provides getter/setter access to an attribute. You can define getters, setters, and delete methods with the property function. If you just want the read property, there is also a @property decorator you can add above your method.
Example
This example shows how to use the @property decorator to create getter, setter, and deleter methods for an attribute in a class, which allows controlled access and manipulation of the attribute -
# create a class class C(object): # constructor def __init__(self, x): self._x = x # getting the values @property def x(self): #I'm the 'x' property. print('Getting x') return self._x # C._x is a property. This is the getter method # setting the values @x.setter #This is a setter method def x(self, x): print('Setting value to ' + x) self._x = x # deleting the values @x.deleter def x(self): print('Deleting x') del self._x # create an object of class y = C('Happy Holidays') print(y.x) #setting the value y.x = 'Merry Christmas!' # deleting the value del y.x
Following is the output obtained -
Getting x Happy Holidays Setting value to Merry Christmas! Deleting x
In Python, objects have both attributes and properties. These two concepts can be confusing, as they are related but not exactly the same thing.
Attributes
Attributes are the variables that belong to an object. We can access them using the dot notation, and they are usually defined within the class definition of the object.
Example
In this example, we define a class called Dog with two attributes: name and age. We then create a Dog object called my_dog and set its name and age attributes to "Fido" and "3", respectively. Finally, we print the value of the name attribute -
class Dog: def __init__(self, name, age): self.name = name self.age = age my_dog = Dog("Fido", 3) print(my_dog.name)
We get the output as shown below -
Fido
Properties
On the other hand, Properties are methods that are used to access or modify the value of an attribute. They are defined using the @property decorator and can be thought of as "getter" methods.
Example
In this example, we define a class called Square with one attribute called side. We also define a property called area, which calculates the area of the square based on its side length. We create a Square object called my_square with a side length of 5 and print its area -
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 result produced is as shown below -
25
It is important to note that properties are not the same as attributes, as they are methods used to access or modify attributes. In the example above, area is not an attribute of my_square, but a property that calculates a value based on the side attribute.
Attributes vs Properties in Python
Let us look at some more examples to better understand the difference between attributes and properties.
Example: Attribute vs Property Example
In this example, we define a class BankAccount with two attributes: balance and interest_rate. We also define a property interest_earned, which calculates the amount of interest earned on the account based on its balance and interest rate. We create a BankAccount object, acct1, with a balance of 1000 and an interest rate of 0.05, and then print out its balance and interest earned using the dot notation -
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(acct1.balance) print(acct1.interest_earned)
Following is the output of the above code -
1000 50.0
In this example, balance and interest_rate are attributes of the BankAccount object acct1. interest_earned is a property that calculates a value based on the attributes of the object. It is defined using the @property decorator and can be accessed using the dot notation.
Overall, the difference between attributes and properties in Python is that attributes are simply data members of an object, while properties are methods that are accessed like attributes but actually perform some calculations when called.
Example
Here is an example that shows the use of attributes and properties in a class representing a rectangle.
In this example, width and height are attributes of the Rectangle object rect. Area and perimeter are properties that calculate the area and perimeter of a rectangle based on its attributes. Both properties are defined using the @property decorator and can be accessed using the dot notation -
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(rect.width) print(rect.height) print(rect.area) print(rect.perimeter)
After executing the above code, we get the following output -
5 3 15 16
Example
Following is another example that demonstrates the use of attributes and properties in a class representing a person.
In this example, name and age are attributes of the Person objects person1 and person2. is_minor and is_adult are properties that check whether the person is a minor or an adult based on their age. Both properties are defined using the @property decorator and can be accessed using the dot notation -
class Person: def __init__(self, name, age): self.name = name self.age = age @property def is_minor(self): return self.age < 18 @property def is_adult(self): return self.age >= 18 person1 = Person("Alice", 23) person2 = Person("Bob", 16) print(person1.name) print(person1.age) print(person1.is_minor) print(person1.is_adult) print(person2.name) print(person2.age) print(person2.is_minor) print(person2.is_adult)
Following is the output obtained -
Alice 23 False True Bob 16 True False