Difference between attributes and properties in Python
Last Updated :
20 Aug, 2020
Class Attribute: Class Attributes are unique to each class. Each instance of the class will have this attribute.
Example:
Python3
# declare a class
class Employee:
# class attribute
count = 0
# define a method
def increase(self):
Employee.count += 1
# create an Employee
# class object
a1 = Employee()
# calling object's method
a1.increase()
# print value of class attribute
print(a1.count)
a2 = Employee()
a2.increase()
print(a2.count)
print(Employee.count)
Output:
1
2
2
In the above example, count variable is a class attribute.
Instance Attribute: Instance Attributes are unique to each instance, (an instance is another name for an object). Every object/instance has its own attribute and can be changed without affecting other instances.
Example:
Python3
# create a class
class Employee:
# constructor
def __init__(self):
# instance attribute
self.name = 'Gfg'
self.salary = 4000
# define a method
def show(self):
print(self.name)
print(self.salary)
# create an object of
# Employee class
x = Employee()
# method calling
x.show()
Output:
Gfg
4000
Now, Let's see an example on properties:
1) Create Properties of a class using property() function:
Syntax: property(fget, fset, fdel, doc)
Example:
Python3
# create a class
class gfg:
# constructor
def __init__(self, value):
self._value = value
# getting the values
def getter(self):
print('Getting value')
return self._value
# setting the values
def setter(self, value):
print('Setting value to ' + value)
self._value = value
# deleting the values
def deleter(self):
print('Deleting value')
del self._value
# create a properties
value = property(getter, setter, deleter, )
# create a gfg class object
x = gfg('Happy Coding!')
print(x.value)
x.value = 'Hey Coder!'
# deleting the value
del x.value
Output:
Getting value
Happy Coding!
Setting value to Hey Coder!
Deleting value
2) Create Properties of a class Using @property decorator:
We can apply the property function by using @property decorator. This is one of the built-in decorators. A decorator is simply a function that takes another function as an argument and adding to its behavior by wrapping it.
Example:
Python3
# create a class
class byDeco:
# constructor
def __init__(self, value):
self._value = value
# getting the values
@property
def value(self):
print('Getting value')
return self._value
# setting the values
@value.setter
def value(self, value):
print('Setting value to ' + value)
self._value = value
# deleting the values
@value.deleter
def value(self):
print('Deleting value')
del self._value
# create an object of class
x = byDeco('happy Coding')
print(x.value)
x.value = 'Hey Coder!'
# deleting the value
del x.value
Output:
Getting value
happy Coding
Setting value to Hey Coder!
Deleting value
Table of difference between Attribute V/s Property
Attribute
| Property
|
---|
Attributes are described by data variables for example like name, age, height etc. | Properties are special kind of attributes. |
Two types of attributes:
- Class attribute
- Instance attribute
| It has getter, setter and delete methods like __get__, __set__ and __delete__ methods. |
Class attributes are defined in the class body parts usually at the top. | We can define getters, setters, and delete methods with the property() function. |
Instance attribute are defined in the class body using self keyword usually it the __init__() method. | If we just want to the read property, there is also a @property decorator which you can add above your method. |
Similar Reads
Python: Difference between dir() and help() In Python, the dir() and help() functions help programmers understand objects and their functionality.dir() lists all the attributes and methods available for an object, making it easy to explore what it can do.help() provides detailed information about an object, including descriptions of its metho
4 min read
Difference between dir() and vars() in Python As Python stores their instance variables in the form of dictionary belonging to the respective object both dir() and vars() functions are used to list the attributes of an instance/object of the Python class. Though having a similar utility these functions have significant individual use cases.dir(
2 min read
Difference Between x = x + y and x += y in Python We often use x += y instead of x = x + y. So, are they same or different? Let's Find it here. Example 1: Python3 x = [1, 2] another_x = x y = [3] x += y print(x) print(another_x) Output: [1, 2, 3] [1, 2, 3] Example 2: Python3 x = [1, 2] another_x = x y = [3] x = x + y print(x) print(another_x) Outpu
2 min read
Difference between "__eq__" VS "is" VS "==" in Python There are various ways using which the objects of any type in Python can be compared. Python has "==" operator, "is" operator, and "__eq__" dunder method for comparing two objects of a class or to customize the comparison. This article explains the above said three operators and how they differ from
4 min read
Dataframe Attributes in Python Pandas In this article, we will discuss the different attributes of a dataframe. Attributes are the properties of a DataFrame that can be used to fetch data or any information related to a particular dataframe. The syntax of writing an attribute is: DataFrame_name.attribute These are the attributes of the
11 min read