Class and instance attributes in Python
What is a OOP
It is a type of computer programming base in four principles:
- Encapsulation: Also known as “information hiding”. The idea is that an object has to provide its users only with the essential information for manipulation, without the internal details.
- Data Abstraction: The process of picking out (abstracting) common features of objects and procedures.
- Inheritance: It’s useful to Eliminate redundant code by inheriting functions and data from other classes.
- Polymorphism: It allow us to treat objects of a derived class (other classes) as objects of its base or actual class .
What is a class
A class is a template that will let us add characteristics, and actions to an object. To create a class, use the keyword class.
Here in the image, we can see the class car is the template to create the objects BMW i8, Tesla 3 and Audi a3, each one will be a different object, but they will share some common properties, and methods.
What is an object
An object sometimes can be the representation of the world, It is an instance of a class, and a self-contained entity that consists of data and procedures (also known as methods or functions) to manipulate the data, it lives in memory as the process exists or until destroyed.
When we create an object, it is usually call the instance of a class, and its in that moment that our objects will have their own names. Like in the example before, the name of the cars.
To get familiar with words here we can clarify them
What is A class Attribute and Instance attributes
Class attributes: are shared variables that can be accessed by all instances of that class. There is only one copy of the class variable and when any one object makes a change to a class variable, that change will be seen by all the other instances.
Object/Instance attributes: are owned by each individual object of the class. In this case, each object has its own copy of the attributes, and they are not shared and are not related in any way to the other instances.
To obtain the attributes of the instance it needs to pass trough a constructor or initializer, which is a function of a class:
def__init__(self, width, height)
What are all the ways to create them and what is the Pythonic way of doing it
Mainly there are two ways to create instance attributes, the conventional method involves placing two underscores for private attributes and just one underscore for protected them, Here an example of the private attribute __size.
The pythonic way to do it is using the property and setter decorators which allows us to attach code to the self.size attribute in the constructor. Any code using the value of size will be called with size in the @property def size, and will be check in the @size.setter for making sure the value size is the one we expect.
Differences between Class and Instance attributes
Advantages and Disadvantages
The advantage of having instance attributes is that you can parse and verify the data that the user passes in before assigning it to the instance of the class. Also you can raise errors if the data doesn’t fit the requirements you set.
The disadvantages for class attributes Is that it’s not possible to have two instances with different values. So you can’t use them to do different things on different objects.
How does Python deal with the object and class attributes using the __dict__
All instances have a __dict__ “dictionary”, which they use to store their information such as attributes and their corresponding values.
Here an example of the information we can get from a class Rectangle, we can see that the information we are getting are just the definition and the location in memory for the attributes height, and width and the method __init__ as well.
But __dict__ could be really useful for viewing the information of an instance, For example, the information below is about the instance my_rectangle, which displays its height and width values.