Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Difference between attributes and properties in Python
Next article icon

Difference between attributes and properties in Python

Last Updated : 20 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Difference between attributes and properties in Python

M

mayanktyagi1709
Improve
Article Tags :
  • Python
  • python-oop-concepts
  • Python-OOP
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences