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'
print(x.__class__.__name__)
print(s.__class__.__name__)

 

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.

x=10
help(x)

 

Out:
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:
    pass

 

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()
print(n)

 

Out:
<__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)
#True

isinstance(1, int)
# True

isinstance('hello', str)
# True

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

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

 

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()
dir(n)


x = 10
dir(x)


s = 'hello'
dir(s)

 

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

['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isascii',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

 

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'
print(s.capitalize())


Out:
'Hello'

 

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

 

Summary

- OOP is a very popular programming paradigm.

- Everything in Python is an object

 


Join the discussion

Share this post with your friends!