Class And Instance Attributes

What is Object Oriented Programming (OOP)

Mohameth Seck
3 min readJun 7, 2019

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.

Another example of classes and objects in this picture

Instance Attribute

An instance attribute is a Python variable belonging to only one object. It is only accessible in the scope of the object and it is defined inside the constructor function of a class. For example, __init__(self,..).

Class Attribute

A class attribute is a Python Variable that belongs to a class rather than a particular object. This is shared between all other objects of the same class and is defined outside the constructor function __init__(self,…), of the class.

Differences Between Class and Instance Attributes

The difference is that class attributes is shared by all instances. When you change the value of a class attribute, it will affect all instances that share the same exact value. The attribute of an instance on the other hand is unique to that instance.

__dict__

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.

Creating Classes and Instance Pythonic and non-Pythonic

A non-Pythonic way would be like this:

class Square:
def __init__(self, size=0):
if not isinstance(size, int):
raise TypeError("size must be an integer")

if size < 0:
raise ValueError("size must be >= 0")

self.__size = size * size
def area(self):
return self.__sizet__width()

The disadvantage comes when we need to modify the code. The pythonic way of doing it would be to use getter and setter property methods.

class Square:
def __init__(self, size=0):
self.size = size
@property
def size(self):
return self.__size
@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
if value < 0:
raise ValueError("size must be >= 0")
self.__size = value
def area(self):
return self.__size * self.__size

The advantage of using property allows us to attach code to the self.size attribute and any code assigned the value of size will be called with size in def size.

Python is not my favorite introduction to object oriented programming, or OOP, but it’s definitely not the worst. Just remember, everything in Python is an object.

--

--

Mohameth Seck

Studying Software Engineering at Holberton School, New Haven, CT. Learning iOS development on the side. VR/AR enthusiast. I just want to make dope stuff 🤷🏾‍♂️