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.