Class and instance attributes

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. Almost all the code is implemented using a special construct called class.

A class : it’s a user-defined prototype for an object that defines a set of attributes that characterize any object of the class.

An instance : is a copy of the class with actual values.

The class by itself is of no use unless it is defined by setting attributes, which act as containers for data and functions related to those attributes, called methods.

A class attribute : is a Python Variable that belongs to a class rather than a particular object. This is shared between all other objects of the same class and is defined outside the constructor function __init__(self,…), of the class.

the output will be the same “i’m a class attribute”

An instance attribute : is a Python variable belonging to one, and only one, instance. it regroups all the unique characteristics that you add to your object and they are not going to affect any other object that you have created. This variable is only accessible in the scope of this object

The pythonic way to create it is inside the constructor function, __init__(self,..) of the class. Additionally, we can define getter and setter methods, which will respectively retrieve the value and set the value of a private instance attribute.

Difference between instances and class attributes

In real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world.

Using object-oriented terminology, we say that your bicycle object is an instance of the class of objects known as bicycles. A class attribute is all the variables or characteristics that this class is going to have: Bicycles have some state (current gear, current cadence, two wheels) and behavior (change gears, brake) in common. However, each bicycle’s state is independent of and can be different from that of other bicycles.

Python deal with the objects and class attributes using the __dict__ that is a dictionary where python saves all the attributes that the object contains, we can use this built-in class attribute to map all the different attributes of an object.

What Are the Advantages and Drawbacks of Class and Instance Attributes?

Advantages of class attributes:

  • They store data that is relevant to all the instances. For example, we could have a counter class attribute that increments every time we create a new instance and decrements every time we delete an instance. This way we can always keep track of how many instances of the same class we created.

Disadvantages of class attributes:

  • you can’t use them to do different things on different objects. if our program countains only class attributes we would not be able to make a list of different objects in it.

Advantages of instance attributes:

  • They are specific to an object and are easy to set and get thanks to properties.
  • They are discarded once the instance is deleted, so they die with the instance they are associated with, which makes things clearer.

Disadvantages of instance attributes:

  • They don’t allow keeping track of values in between instances.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium


5 megatrends shaping the future of testing automation

Unity vertex/fragment shader02 texture URP

Getting Started with STL

Become an ambassador of the Cloud team, complete missions in the Kronos galaxy and always win.

Getting started with Django Web Framework and Docker

How to start with Python ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eya Nani

Eya Nani

More from Medium

Middleware Functionality To Allow/Restrict User Access

Solving OneFourtySix Case

The Software Initiative (Part 3)

Honeypot Project