DEV Community

dillybunn
dillybunn

Posted on

Object Oriented Programming

Python as a language offers its users a wide range of uses and benefits, but one of the most important is how it handles objects. It can get confusing, thinking about objects as code and not physically manifested but that is where Object Oriented Programming comes into play.

Object-oriented programming (OOP) is an important programming paradigm that is based on the concept of "objects," which can contain data in the form of fields (attributes or properties) and code in the form of procedures (methods). OOP focuses on creating reusable and modular code, which can lead to more efficient and maintainable software.

Now that sounds technical but what does it mean. Simply put, objects in OOP are instances of classes, which define their structure and behavior.

With OOP we can write code that sets up a class and then allows us to update very easily, let's see how.

class Player:
    def __init__(self, name, position, batting_average, home_runs):
        self.name = name
        self.position = position
        self.batting_average = batting_average
        self.home_runs = home_runs
    def update_stats(self, new_batting_average, new_home_runs):
        self.batting_average = new_batting_average
        self.home_runs = new_home_runs
    def display_info(self):
        print(f"Name: {self.name}, Position: {self.position}, Batting Average: {self.batting_average}, Home Runs: {self.home_runs}")
Enter fullscreen mode Exit fullscreen mode

This piece of code sets up a class for a baseball player. It gives us the ability to manipulate the data tied to the player such as name, position, and their stats (batting average, home runs etc.)

Now we can create instances of a player and assign the stats to them and call them to see the attributes.

player1 = Player("Pete Alonso", "First Basemen", 0.250, 20)

print(player1.batting_average) #Output: .250

Enter fullscreen mode Exit fullscreen mode

But OOP is more then just a way to track stats. We can set up our code to make sure we are getting the correct return by making sure it is instance. Homeruns should be a number. How can we make sure that we are getting the right thing back? Simple we use isinstance.

Using our code from before:

player1 = Player("Pete Alonso", "First Basemen", 0.250, 20)

if isinstance(player1.home_runs, int):
     print(f"{player1.name} has hit {player1.homeruns} homeruns")
else:
     print(f"{player1.name},s home_runs attribute is not an integer")
Enter fullscreen mode Exit fullscreen mode

We now know that when trying to do something with Pete Alonso's homerun totals, we will always get back a number.

The advantages of using Python and OOP are multifaceted and go well beyond the basic example we used here today. We can update Pete's stats using def update_stats , we can make sure position players and pitchers have their own separate statistics (I wouldn't expect many pitchers to hit the homeruns!) and we can continue to build this code out to simulate an entire game!

And that is the real strength of OOP. By creating reusable and easy to read code, you start with a strong foundation to build upon, adding in checks and balances to make sure you aren't getting junk information returned back to you. It may be a very basic overview, but even with that small glance, we can see how powerful it really is.

Top comments (0)