Python Basics: Object and Class Attributes

Sergio Pietri
4 min readJan 15, 2020

--

Objects & Classes

As you may already know, Python is a OOP oriented language, and while you can use it to program procedurally it is best used with the aforementioned programming paradigm. This means that it works best by organizing the data properly and creating classes so the code can be encapsulated and later reused for max efficiency and to avoid repeating unnecessary code blocks. All this can be achieved thanks to the objects and the classes you create along with their attributes and characteristics so let’s take a quick look as to what are some of the differences between them.

Class and Class attributes

A class goes hand in hand with the encapsulation principle of object oriented programming in which each object should be restricted to a certain field of action. A class is that field and the objects are the ones who play in it. Think of the class as a blueprint to build something, the most usual example is a house blueprint.

The blueprint can contain the materials needed to build it, the distribution, the amount of rooms, and the color of the house. You could use one blueprint to build several houses and you could maybe paint them in different colors or change the amount of rooms but the core build of the house will come from that blueprint you have.

This is what happens with classes. You can create a class to build something and then use it as many times as you like to build new things with it and those things can be the objects.

When it comes to the attributes of a class in Python you will find that they refer to variable that belongs to the class and not to a particular object (like inside a method of the class). The attributes are defined outside of any object and before the constructor of the class (identifiable in Python by the __init__ method).

They are shared by all the other objects inside the class like if they were a global variable.

The image above shows how we declare a class called ‘ClassDeclared’ that contains an attribute called ‘class_variable’ and is initialized to 0. Later we can find the constructor identified by the __init__ method.

Objects and Object attributes

There’s a lot of talk about objects but what exactly are they? This is an abstract concept since it doesn’t refer specifically to a physical object, but one could say that everything is an object in Python.

An object is a collection of data with a particular objective, it can be a method (known as a function in procedural programming) or a variable.

In Python, you can use classes to create objects of that type, think of it as if you were declaring a variable in C where you declare the variable type with ‘int’ and then you give it a name. Here, you can do something similar with classes.

What happend here is that we just created an object called ‘object’ that will contain the atributes and methods contained in ‘ClassDeclared’. Our object will now inherit the class characteristics so if this was a house we would have just created a new house with all the details contained in the blueprint (the class).

Difference Between Object and Class attributes:

The main difference between both, other than the fact that objects and classes share different concepts, is that an object (or instance) attribute refers only to a variable that belongs to an object and is declared inside the __init__ method of your class, most commonly known in other languages as a constructor.

On the other hand, a class attribute is a variable that must be declared outside of any other object (meaning methods in this case) so that it belongs to the class in general; it is almost like a mini global variable that can be initialized with a desired value or character to be used in any method (object) inside the class.

The main advantage that these attributes can have is their ability to be used and reused whenever the programmer desires to do so, meaning less code must be rewritten and not all of it has to be imported. You could simply use certain attributes that work in any specific case for a task that you must develop or use certain methods for precise functionality.

But not everything is rainbows and butterflies, a drawback in this case is that some times it can prove hard to keep track of every class and method you created so everything must be kept neatly organized and with proper notes. Also, but not anything significant, the concept can be harder to grasp due to its abstractness than a procedural type of coding.

Object Oriented Programming can be a tricky concept to handle since it mostly consists of very abstract content, but in practice this is the most effective way to code specially in a team environment. Knowing the difference between instances and classes can help solidify the OOP paradigm in your mind so you can apply it easier in your real-life scenarios and the concepts discussed here are part of that core you must begin to learn.

I hope you have a great day!

S.

--

--

Sergio Pietri

Backend Developer. I like to write about tech and other stuff. Follow me on twitter for more content!