Class and instance attributes
Before I start talking about classes and instances, I would like to share a brief introduction about what object-oriented programming is.
Object oriented program
When we are programming and we design our programs around functions i.e. blocks of statements which manipulate data, this is called the procedure-oriented way of programming.
There is another way of organizing your program, which is to combine data and functionality and wrap it inside something called an object. This is called the object-oriented programming paradigm.
Classes and Instances attributes
In the object-oriented programming we have two main aspects:
• Classes
• Objects or Instances
A class creates a new type where objects are instances of the class. A good analogy would be thinking about variables of type float it would be the same as saying that variables that store floats are variables which are instances (objects) of the float class.
Objects can store data using ordinary variables that belong to the object. The variables that belong to an object or class are referred to as fields.
So, fields could be of two types:
• Instance - if they belong to each instance/object of the class.
• Class - if they belong to the class itself.
They are called instance variables and class variables respectively.
Objects also have functionality by using functions that belong to a class. These functions are called methods of the class.
This terminology is important because it helps us to differentiate between functions and variables which are independent and those which belong to a class or object.
Collectively, the fields and methods can be referred to as the attributes of that class.
Instance Attributes: are owned by each individual object/instance of the class. In this case, each object has its own copy of the field i.e. they are not shared and are not related in any way to the field by the same name in a different instance.
Class Attributes: are shared - they 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.
How to create classes and instances
A class is created using the class keyword. The fields and methods of the class are listed in an indented block.
For example, if we want to create the class Square, our code should be:
The next step is to create a method.
Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method, Python will provide it. This particular variable refers to the object itself, and by convention, it is given the name self.
There are many method names which have special significance in Python classes. We will see the significance of the __init__ method now.
The __init__ method is run as soon as an object of a class is instantiated (i.e. created). The method is useful to do any initialization (i.e. passing initial values to your object) you want to do with your object.
So, following with our example we define the init method:
Here, we define the __init__ method as taking a parameter size (along with the usual self).
We just created a new field also called size. Notice these are two different variables even though they are both called 'size'. There is no problem because the dotted notation self.size means that there is something called "size" that is part of the object called "self" and the other size is a local variable. Since we explicitly indicate which size we are referring to, there is no confusion.
When creating new instance for example my_square, of the class Square, we do so by using the class name, followed by the arguments in the parentheses: my_square = Square(3).
Ok, we have seen how to create a class and how to create an instance with his initial attribute. Now, I want to show you how to create a class attribute and how to use it.
Following our example, I’m going to create the class attribute number_of_instances which is initialized in 0 and incremented during each new instance instantiation.
Here, number_of_instances belong to the Square class and is a class variable. The size variable belongs to the object (it is assigned using self) and is an object variable.
Note that we refer to the number_of_instances class variable as Square.number_of_instances and not as self. number_of_instances. We refer to the object variable name using self.name notation in the methods of that object. Remember this simple difference between class and object variables. Also note that an object variable with the same name as a class variable will hide the class variable.
Differences between class and instance attributes - advantages and drawbacks of each of them
Advantages of class attributes:
•They are shared to all the instances of the class
•We can do operations and data management throughout all the objects at the class.
Advantages of instance attributes
• They are specific to an object.
• They can coexist with class variables while having the same name
Disadvantages of class attributes:
• Class attributes can become messy when instance attributes with the same names are created, meaning that its behavior can become unexpected.
Disadvantages of instance attributes:
• They cannot be directly accessed by another instance if needed.
• Once the instance is deleted, its attributes are gone.
How does Python deal with the object and class attributes using the __dict__
In Python, all instance variables are stored as a regular dictionary. When working with attributes, you just changing a dictionary.
We can access instance dictionary by calling __dict__:
By knowing this detail, we can save and later restore the state of an arbitrary class:
As I said before, class attributes are owned by a class itself. As it turns out, classes are using a dictionary too.
Class dictionary can also be accessed from an instance, using __class__ dunder method (i.e., car.__class__.__dict__).
Dictionaries of classes are protected by mappingproxy. The proxy checks that all attribute names are strings, which helps to speed-up attribute lookups. As a downside, it makes dictionary read-only.
Because all methods belong to a class, they are also stored in this dictionary.
Software Developer
2yGood ,,,,,,👌