Python Object Oriented Programming Overview
A Walkthrough of Creating Classes, Attributes, and Methods in Python
Introduction
Object oriented programming (OOP) is a programming paradigm based on the concept of “objects.” This article will focus on OOP in Python, which is just one example of an OOP language along with Java, JavaScript, C++, C#, R, etc. Object oriented programming basically enables programmers to create their own, unique classes and objects with corresponding attributes and methods. Overall it is very useful for keeping large and complex code organized and repeatable.
Within Python, objects are specific instances of a class. Several examples of built-in Python classes are strings, lists, dictionaries, etc. For example when we use the built-in type() function on a string we can see that it returns the ‘str’ class at the bottom of the cell as seen below.
Alternatively when we use type() on a list, we can see it returns the ‘list’ class as seen below.
Using these objects we are able to call certain attributes and methods from them. Attributes can be thought of as simply returning information about the object and don’t use a parenthesis at the end when calling them. We will see examples of attributes later in this article. Methods on the other hand are basically functions associated with these objects that can be used for returning results or changing the current object based on information from the object. They can basically be thought of as performing some kind of action on an object. For example, using the built-in .append() and .pop() methods on a list or using the .key() or .values() methods on a dictionary, as seen in the examples below.
Creating User-Defined Objects
In order to create a user-defined object in Python we first need to use the ‘class’ keyword. A class can basically be thought of as a blueprint defining the specific nature of an object. We can then construct instances of an object from a particular class. In the example below I start off by creating a class for NFL teams. We begin by typing the class keyword followed by the title of the class (FootballTeams). It should be noted that class names are denoted using CamelCasing rather than snake_casing in Python. We can then create an instance of this class by setting a variable to the class name followed by parenthesis. Now once we check the type of this class we can see that it returns the .FootballTeams class.
Creating Class Attributes
The next step in creating a class is to create attributes. We do this by first creating a function followed by the ‘init’ method in order to initialize the new class. Always be sure to wrap the ‘init’ keyword in double underscores when creating a class as seen in the example below. The ‘self’ keyword then connects this method to the instance of the class or can basically be thought of as referring to itself. We can then pass in attributes for the user to define. For example, every NFL team will have a city name and quarterback so we will first pass in ‘qb’ and ‘city’ as parameters, again always using the ‘self’ keyword before each attribute in order to connect it to the class. Now whenever we call the ‘.city’ and ‘.qb’ attributes it will return whatever we set for those parameters.
Parameters also don’t always need to be strings, we can also add other classes such as Booleans or integers for example.
We can also define attributes at a class-object level, for example since we know every NFL team is located in the United States we can add a class-object attribute which will be the same for any instance of a class. We do this above the init method and don’t need to use the self keyword because it isn’t refering to any specific instance of the class but rather the entire class.
Now that we have our .FootballTeams class blueprint we can create another instance of the class for another NFL team by passing in the new parameters as seen below.
Creating Class Methods
Now that we’ve covered attributes, we will now move on to creating methods in OOP. As previously mentioned, methods can be thought of basically as functions within a class that perform some sort of action or operation within the class rather than only returning information as attributes do. It’s also important to remember that methods always have an open and closed parenthesis at the end of them, similar to standard functions. In order to create a new method we simply create a function within the class, making sure to pass in the self keyword in order to connect it to the particular instance of the class as seen in the example below.
Now whenever we call the .qb_sneak() method, it will return the print statement and f-string as seen in the above code. It should also be noted to be sure to include the ‘self’ keyword in the f-string in order to connect it to the instance of the class.
Just as with regular functions we can also pass in arguments to methods. For example if we wanted to create a postseason conditional statement while passing in the actual name of the team rather than the city we can do so by passing in a ‘team’ argument.
Now when we run this newly created method we get the following,
Conclusion
Hopefully with the examples above you can start to see the many benefits of using Object Oriented Programming in Python through the use of creating classes, attributes, and methods. OOP is a great way to keep your code organized and readible. It also is great for making code easily repeatible for large scale programs and projects. Overall it is an extremely powerful tool that helps to make writing complex programs much more manageable for developers in a variety of coding languages.