Class and instance attributes

Daniel felipe Escobar chavez
4 min readMay 21, 2021

--

In object-oriented programming (OOP) it is a paradigm, this means that it is a programming style that gives us its own guidelines to work with the structuring of the data that the so-called: classes have to store. In this type of programming we can find two tools that will be quite useful and they are the object and class attributes.
attributes can be viewed as variables but can only exist within an object.

Before we start we are going to define two basic concepts to talk about this topic and they are classes and instances. Classes group objects with properties and characteristics in common, for example Names. and the instances try to create an object from a class. finally the attributes and methods are elements that store data. With all this clear, we are going to see how the class and instance attributes work.

Class attributes:

An instance attribute is a Python variable belonging to one, and only one, object. This variable is only accessible in the scope of this object and it is defined inside the constructor function, __init __ (self, ..) of the class.
here I show how this attribute works:

class Students: 

nombre = 'Andres'

def __init __ (self, classroom, age):
self.age = age
self.classroom = classroom

Object attributes:

A class attribute is a Python variable that belongs to a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the constructor function, __init __ (self, …), of the class.
Here is an example of this attribute:

class Students: 


def __init __ (self, classroom, age):
self.age = age
self.classroom = classroom

Differences:

- Among their differences, the most key is that the class attributes are shared by the entire class instance while the object attributes are particular for each object created in the same class, therefore the instance variables are for unique and proper data of each object and class variables are for attributes that must be shared by all instances of the class.

The Pythonic way of doing it:

the correct way to handle our stributs and it is with getters and setters,
a getter is a method that obtains the value of a property, which in OPP helps us to access private attributes of a class.

A setter is a method that establishes the value of a property, in OPP it is used to establish the value of private attributes in a class.
With these two methods we can ensure encapsulation between other things such as:
-keep the interface consistent
-To add validation logic to get and set a value
-To avoid direct access or modification of a class field

now we will see an example of how we use them in the code:

class Students:
"""comments"""

nombre = 'Andres'

def __init__(self, classroom, age):
self.age = age
self.classroom = classroom
@property
def classroom(self):
return self.__classroom
@classroom.setter
def classroom(self, value):
self.__classroom = value
@property
def age(self):
return self.__age
@age.setter
def age(self, value):
self.__age = value

Here we can see how getters and setters work because here the getter is defined with @property and it helps us to obtain the value of an element privately while the setter assigns values and it can also be used to use ecxept and find errors.
so when you want to create a class in python you can use this structure.

advantages and disadvantages:
The advantage of using instance attributes is that since they are specific to an object they cannot be changed so it can benefit when you try to do specific things without the need for changes, the disadvantage of this is that if at some point we must change something we will not be able to so in this case it is better to use a class attribute since it gives us more dynamism when declaring the elements we need in the class.

How does Python deal with the object and class attributes using the __dict__:

Module objects have a read-only secret attribute called __dict__ which returns the dictionary used to implement the module namespace, here we will see an example of how it works:

def func():
pass

func.temp = 1

print(func.__dict__)

class TempClass:
a = 1
def temp_function(self):
pass

print(TempClass.__dict__)

and the result is:

{'temp': 1}
{'__module__': '__main__',
'a': 1,
'temp_function': <function TempClass.temp_function at 0x10a3a2950>,
'__dict__': <attribute '__dict__' of 'TempClass' objects>,
'__weakref__': <attribute '__weakref__' of 'TempClass' objects>,
'__doc__': None}

now let’s try to organize this with our logic:

the_student = Students("AB", 11)
print(the_student.__dict__)

and the result is

{‘_Students__classroom’: ‘AB’, ‘_Students__age’: 11}

with all this we have a good idea about OOP in python.
that concludes everything for today, until next time!

--

--

No responses yet