What is OOP?

by John | November 25, 2020

What is OOP and Why Learn it?


Object Orientated programming is a programming paradigm that involves writing blocks of code in a structure known as a class. These classes contain information and methods for which instances of the classes will inherit when they are created. This is a very useful way to structure code that interacts with other objects. A good way to think about these classes, is that they are a collection of related data and functions. 


In regards to OOP in Python, everything is in fact an object when programming in Python. Let's take a a few very simple example using an integer and a string. 


x = 10
s = 'hello'


If you run the lines of code above, you will see it returns 'int' and 'string'. These are the classes from which we are creating instances. Therefore 10 is an instance of the int (integer) class and 'hello' is an instance of the str (string) class. 


What does a class look like? 

Continuing with the example from the int class, we can have a look at the class and the functions that the class contains using the help command.



class int(object)
 |  int([x]) -> integer
 |  int(x, base=10) -> integer
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is a number, return x.__int__().  For floating point
 |  numbers, this truncates towards zero.
 |  If x is not a number or if base is given, then x must be a string,
 |  bytes, or bytearray instance representing an integer literal in the
 |  given base.  The literal can be preceded by '+' or '-' and be surrounded
 |  by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
 |  Base 0 means to interpret the base from the string as an integer literal.
 |  >>> int('0b100', base=0)
 |  4
 |  Methods defined here:



Therefore to create a class in Python we should use the following syntax:


class Name:


The class part from above is a keyword to let Python know we are creating a class, and the Name is a custom parameter used to name the class to something meaningful. To create an instance of the class we can use the following:

n = Name()


<__main__.Name object at 0x0000014ADA6ABF88>


If we want to check whether an object is an instance of any given class we can use the built-in isinstance method. 


isinstance(n, Name)

isinstance(1, int)
# True

isinstance('hello', str)
# True

isinstance([1,2,3], int)
# False

isinstance([1,2,3], list)


As mentioned at the start  of the this document, you can think of a class as an object that contains a collection of related data and functions, we can use Python's built in dir method to have a look at these methods and data:

n = Name()

x = 10

s = 'hello'


Looking at the output from the string class we see the following:



If you have ever been wondering why some of Python's built in methods seem to work like magic, this is the reason why. Take for example the capitalize method above:


s = 'hello'



So we see that the method related to capitalizing strings is included when we create an instance of a string. 



- OOP is a very popular programming paradigm.

- Everything in Python is an object


Join the discussion

Share this post with your friends!