Difference between attributes and properties in Python - GeeksforGeeks

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 classclass Employee:         # class attribute    count = 0                  # define a method    def increase(self):         Employee.count += 1    # create an Employee # class objecta1 = Employee()   # calling object's methoda1.increase()   # print value of class attributeprint(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 classclass 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 classx = Employee()  # method callingx.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 classclass 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 objectx = gfg('Happy Coding!') print(x.value)     x.value = 'Hey Coder!'    # deleting the valuedel 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 classclass 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 classx = byDeco('happy Coding') print(x.value)     x.value = 'Hey Coder!'    # deleting the valuedel x.value
Output:
Getting value
happy Coding
Setting value to Hey Coder!
Deleting value
Attribute
Property
Two types of attributes:
Class attribute
Instance attribute
Python-OOP
python-oop-concepts
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python