Hello! This is just a Mid-Week post on a subject I wanted to talk more about: Object Oriented Programming.
I actually didn't realize that for one of our 148 assigned SLOG topics, we were supposed to talk IN-DEPTH about OOP so I wanted to address it at least once. Oops!
Just to recap from my first SLOG, OOP is completely oriented on the creation and manipulation of objects as opposed to Procedural Programming which is oriented on the creation of functions with how they manipulate data.
Our starting point is the creation of a
class which is like a blueprint to help us with the creation and usage of objects.
Afterwards, we can start defining methods of the class, which are functions that work specifically for the class's objects. With our newly defined methods we can now alter our objects in however we please or retrieve specific information about the object.
To clarify things, here's one example I recall from our 148 lab:
We can create a vehicle object that travels from place to place as long as it has enough fuel. How do we make Python do this?
Step 1: Create our vehicle class. (For example: class PublicBus)
Step 2: Initialize the vehicle class with default settings you would like to give it. Current fuel, fuel efficiency and current position are good examples of how vehicles are like in real world situations.
Step 3: Create our methods to make changes to our objects. Perhaps we'd like to fill up on gas, so how about a fill_up_gas method? Also we'd like to go from point A to point B, so how about a travel_to method? Of course our vehicle will need to meet the gas requirement so we can also add this in our travel_to method or create a helper method to check it for us.
Finally, we have done all we needed to and can start creating the actual object. It's quite simple actually. Just create an object using your blueprint (the vehicle class) like this: new_vehicle = PublicBus(100, 5).
100 here stands for the fuel, 5 stands for the fuel efficiency. These are the user-defined attributes of your new Public Bus object.
To get a bit technical, here is how it looks in Python code:
class PublicBus:
"""A PublicBus object. """
def __init__(self, current_fuel, fuel_efficiency):
""" Initializes PublicBus objects. """
# Note: self refers to the object, it is named self by convention and is always the first parameter for class methods.
self.current_fuel = current_fuel
self.fuel_efficiency = fuel_efficiency
self.max_tank_capcity = 100
self.current_pos = (0, 0)
# Notice how current_pos isn't in the __init__ parameter? This is because current_pos will ALWAYS start at (0, 0) coordinates when creating a new PublicBus object.
def fill_up(self):
""" Fills up the current fuel to the tank's max capacity. """
self.current_fuel = self.max_tank_capacity # This is one of the class methods that will alter your object's attributes!
def travel_to...
def ....... so on, so forth. Keep defining methods until the object can meet all your requirements.
Now we can actually start using our object. Here is an arbitrary example that demonstrates how objects are created, used and how their attributes are changed.
NOTE: We use new_vehicle. (dot) in the code to call on the methods or check the class variables of the object's class. In this case, new_vehicle is the object and it's class is PublicBus.
>>> new_vehicle = PublicBus(100, 5) # creating new object!
>>> new_vehicle.current_fuel # checking object variable
100 # variable value
>>> new_vehicle.current_pos
(0, 0)
>>> new_vehicle.travel_to(5, 10) # class method!
>>> new_vehicle.current_pos
(5, 10)
>>> new_vehicle.current_fuel
73 # new variable value!
>>> new_vehicle.fill_up() # class method!
>>> new_vehicle.current_fuel
100 # class method fill_up() working properly.
This concludes the demonstration of how objects are interacted with and created in Python.
As you can see, OOP is totally emphasized on manipulating and creating objects. We have created an object, checked its' attributes, defined methods for it, initialized it and manipulated it all under one class.
Of course this is only one little object though. If we actually created a class for every type of real-life vehicle, imagine all the knowledge we would have of them defined within our code. This is why OOP is important, it allows us to keep track of tons of information and change them as they are required; whether it be for games, medical, vehicles or any other purpose you can think of.
I hope this has been a good read for everyone.
Come back soon for more!