- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the 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 are special kind of attributes which 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.
# 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
Output
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. They can be accessed using the dot notation, and they are usually defined within the object's class definition.
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, which is "Fido".
class Dog: def __init__(self, name, age): self.name = name self.age = age my_dog = Dog("Fido", 3) print(my_dog.name)
Output
Fido
Properties
Properties, on the other hand, 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, which is 25.
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)
Output
25
It's 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 rather a property that calculates a value based on the `side` attribute.
Attributes vs Properties in Python
Let's look at some more code examples to better understand the difference between attributes and properties.
Example: Attribute vs Property Example
In this example, we define a class called `BankAccount` with two attributes: `balance` and `interest_rate`. We also define a property called `interest_earned`, which calculates the amount of interest earned on the account based on its balance and interest rate. We create a `BankAccount` object called `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)
Output
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 object's attributes. 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 computation when called.
Example
Here is an example that demonstrates the use of attributes and properties in a class representing a rectangle −
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)
Output
5 3 15 16
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 the rectangle based on its attributes. Both properties are defined using the `@property` decorator and can be accessed using the dot notation.
Example
Here is another example that demonstrates the use of attributes and properties in a class representing a person −
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)
Output
Alice 23 False True Bob 16 True False
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.