OOP basics with Python

Class and instance attributes

Nour Ouhichi
Analytics Vidhya
Published in
3 min readMay 25, 2020

--

Since Python is an excellent object-oriented programming language, it is urgent to understand the concept of OOP and acquire some basics in it as an introduction.

Let’s answer to what? and why?

What is OOP?

“Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).”

-Wikipedia

Why learning OOP?

Working with objects you define is known to be more effective and simple in terms of software expansion and maintenance complexity. In this context comes Python’s OOP concept which focuses on creating reusable code ; A concept AKA DRY (standing for don’t repeat yourself).

ABC’s

A class:

Simply put, a class is a type, it consists in gathering attributes of all objects created from that class.

An object:

An existent entity with a behavior, for example an object is an Iphone with its color and price while it is able to perform some tasks like making phone calls or playing games.

Let us talk in OOP terms, the class in this example is phone. The object Iphone here is an instance of the class phone. This instance has special data or also called attributes (color, size, price …) and behavior also called methods (phone calls, game playing, voice recording …).

Class attribute:

A variable which belongs to the whole class; objects within the same class are sharing this attribute and so it has to be defined outside the constructor function (__init__(self,..)..).

Instance attribute:

Unlike the class attribute, it is a variable which belongs to a single object , thus it has to be defined inside of a constructor function.

Differences between class and instance attributes:

Unlike the class attribute, an instance attribute, if changed, will only affect the instance itself and not the whole class meanwhile the first one results in modifying all instances within the same class.

The ways to create them and what is the Pythonic way of doing it:

Here is an example of a non-Pythonic way:

class Employee:def __init__(self, first, last):
self.first = first
self.last = last
self.self.email = first + '.' + last + '@email.com'
emp_1 = Employee('Jim', 'Carry')
print(emp_1.first)
print(emp_1.email)
emp_1 = 'Jimmy'
print(emp_1.first)
print(emp_1.email)

The problem in this code is that if it happened that we wanted to modify the first name attribute, the modification will not affect the email attribute so we will have a wrong output:

Jim
Jim.Carry@email.com
Jimmy
Jim.Carry@email.com

Here we state the intervention of the Pythonic way which uses the property decorators , and the setter method attaches the code to the attribute in order to keep it updated with new modifications, ti would look like the following:

class Employee:def __init__(self, first, last):
self.first = first
self.last = last
self.email = first + '.' + last + '@email.com'
@property
def email(self):
return self.email
@email.setter
def email(self):
self.email = self.first + '.' + self.last + '@email.com'
emp_1 = Employee('Jim', 'Carry')print(emp_1.first)
print(emp_1.email)
emp_1.first = "Jimmy"
print(emp_1.email)

This would give the following right output:

Jim
Jim.Carry@email.com
Jimmy
Jimmy.Carry@email.com

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

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element.Each instance is stored in a dictionary with one or more attributes.

--

--

Nour Ouhichi
Analytics Vidhya

Software engineering student at Holberton school with the passion of sharing my knowledge. Simplified language articles about different programming languages.