Class and instance attributes

Smith Flores
4 min readOct 11, 2021

Object Oriented Programming is not a programming language, but rather a model in which programs are organized around data or objects. OOP uses the concept of objects and classes. A class can be thought of as a ‘blueprint’ for objects. These can have their own attributes (characteristics), and methods (actions they perform). In Python everything is an object. Objects are a representation of real world objects like cars, dogs, house, etc. They all share data and behavior.

As an object oriented language, Python provides two scopes for attributes: class attributes and instance attributes. Think of a Class like a blueprint from which different objects are created with the same blueprint.

Each object, or in this case a car, is an instance of class car (image below). The cars can have data like number of wheels, doors, seating capacity, and behaviors: accelerate, stop, display how much fuel is left and more. Data in a class are called attributes and behaviors are called methods.

Data → Attributes & Behavior → Methods

Again, a class is like a blueprint for which other objects can be created. So with class instrument, objects like piano and guitar can be made. From guitar other objects can be made from it and they also can inherit attributes and methods from guitar and instrument if applied to them.

Class Attribute vs. Instance Attribute

  • 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.
  • 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.

The below ExampleClass is a basic Python class with two attributes: class_attr and instance_attr.


A __dict__ is a dictionary or maping objet used to store an object’s attributes. How does Python deal with the object and class attributes using the __dict__ ? Well, each instance is stored in a dictonary.

A class, functions as a template that defines the basic characters of a particular object. Here’s an example:

There are few things to note when looking at the above example.

  1. The class is made up of attributes(data) and methods(functions).
  2. Attributes and methods are simply defined as normal variables and functions.
  3. As noted in the corresponding docstring, the __init__() method is called the initializer . It’s equivalent to the constructor in other object oriented languages, and is the method that is first run when you create a new object, or new instance of the class.
  4. Attributes that aply to the whole class are defined first, and are called class attributes.
  5. Attributes that aply to a specific instance of a class (an object) are called instance attributes. They are generally defined __init__(); this is not necessary, but it is recomended (since attributes defined outside of __init__() run the risk of being accessed before they are defined).
  6. Every method , include in the class definition passes the object in question as its first parameter. The word self is used for this parameter (usage of self is actually by convention, as the word self has no inherent meaning in Python, but this is one of Python’s most respected conventions, and you should always follow it).
  7. Some of the class’s methods have the following form: __FunctionName__(self, other stuff). All such methods are called “Magic methods ” and are important part of classes in Python.

Life is a preparation for the future, and the best preparation for the future is to live as if there were none.

Albert Einstein

I hope you have enjoyed reading the content))