What is object oriented programming?
‘Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.’ — Alexander S. Gillis & Sarah Lewis
In Python, everything is an object. Each object contains its own attributes and methods. How do we know this? Check this out:
x = 'string 'x.capitalize() #will be 'String 'x.isalpha() == True #will be Truex.rstrip() #will be 'string'
In the example about we created a string, known by the variable name, x. A string is a datatype in Python, but it is also a class. Classes dictate the properties and abilities of an object (in this case, x). Capitalize(), isalpha(), and rstrip() are 3 methods that are specific to a string and the string class itself. You cannot use these on integers or floats, which by the way are their own classes as well.
The variable we created, x, is known as an instance. An instance is a declaration of an object, belonging to a specific class. By instantiating an object, the object has then acquired the properties and abilities of that class.
Those datatypes seem to be somewhat of a special case, is there another example that would be possibly more clear?
The best example of this that I can think of is the module NumPy. As Python-based data scientists, we use this package to efficiently manage numerical data. After we import the module, we can use it to create arrays. By creating an array, we are creating an instance of the numpy.ndarray class.
import numpy#this is creating an instance of the numpy.ndarray class
numbers = numpy.array([1, 2, 3, 4])
Now that we have our instance, we can check out its attributes and methods to see what we can do with our array. For reference check out the NumPy documentation. Remember that methods have parenthesis and attributes do not. Attributes are the features of the object, and methods are the actions we can perform on the objects themselves. Here is an example:
numbers.mean() #will return 2.5, THIS IS A METHODnumbers.dtype #will return dtype('int64'), THIS IS AN ATTRIBUTE
Why is all of this important?
It is important to know about classes because as programmers, we have the ability to create our own and make instantiated objects with their own specified capabilities. This is how programs and modules can be made. Here is a simple example of how to go about doing this:
#creating the class IceCream
class IceCream(): #__init__ is a special function that helps define attributes
#and self is a special parameter that must be included.
#We can also put in other parameters to be specified. For
#this we have to specify the flavor and the number of
#tubs in stock def __init__(self, flavor, tubs):
self.flavor = flavor
self.tubs = tubs
#update_stock is a method this will update the tubs count
#as more get sold. The tubs_sold parameter below is
#the number sold. def update_stock(self, tubs_sold):
self.tubs = self.tubs-tubs_sold
if self.tubs == 0:
self.tubs = 'Out of Stock'----------------------------------------
#To use this we simply do the following:vanilla = IceCream(vanilla, 10) #instantiating an object 'vanilla'vanilla.flavor #ATTRIBUTE returns 'vanilla'
vanilla.tubs #ATTRIBUTE returns 10 vanilla.update_stock(5) #METHOD. It doesn't return anything!vanilla.tubs #ATTRIBUTE. It is updated from the method and is now 5.
The example above is a program we created ourselves to keep track of the inventory of ice cream in a store. Programs/modules can be made as simple as this and object oriented programming allows us to get there.
To wrap up, objects and classes along with attributes and methods are what makes up a great amount of what we do when programming in Python. Knowing how these work will help us to get a better understanding of the packages, modules, and programs we work with as data scientists. Also, with knowing this and getting practice with certain syntax specifications, we have the power to create our own programs. This is the first step in understanding how to do it. If Python is not your language of choice, this may still apply, as other languages like Java and Ruby are also considered OOP languages.
OOP Languages — https://readdive.com/top-5-object-oriented-programming-language/
NumPy Documentation — https://numpy.org/doc/