Understanding Classes and Methods in Python

Jared Delora-Ellefson
Analytics Vidhya
Published in
4 min readJun 10, 2020

I originally come from a functional programming background, spending most of my time in older legacy codes like FORTRAN or the god awful lands of PERL. When I was finally forced to learn Object Oriented Programming (OOP), I struggled. Here I will outline what OOP is by using an example in the hopes that I will convert you.

OOP is a popular and powerful programing paradigm. It provides a useful structure for grouping tasks and data into packages. These Objects can be called upon to perform actions or provide whatever data they have. These Objects are similar to functions, but have additional useful properties that functions do not possess.

While often called Objects, which is where the first “O” comes from is OOP, they are usually referred to as Classes. A Class contains two things: Attributes (data) and Methods (similar to functions).

To understand how OOP works, let’s build a Class with some Attributes and then add a simple Method. We’ll build a Class that takes a list of numbers.

Figure 1: Initialization statement for a Python class

The above code shows the initialization statement for our python Class. First we name our Class stat_calc. We then define a few Attributes that we know we will want to often use. In this case we’re just doing a few numpy calculations, but you can initialize just about anything as an Attribute. I often initialize lists, dictionaries, and pandas DataFrames as Attributes.

Now that we have created our Class we are free to use our attributes in our Methods. What are Methods? Methods are akin to functions the Class can perform. We will build one to calculate standard deviation. Numpy could easily give us the answer, but we’ll build a Method to do it.

Figure 2: Standard Deviation for a Population Sample
Figure 3: Example of a python Class Method

In the above piece of code we have added a Method called standard_dev. This Method takes one argument: self. All python Methods must at least have self specified as an input, but the Method can accept additional arguments in the same manner the Class initialization took a list. This Method uses many of the Class Attributes we created when we initialized our Class. The logic of the code matters less than seeing how our Class Attributes can be used throughout the Class Method.

Figure 4: Create an instance of our Class, pass it a list, and print the Class Attributes

To use the Class we have created, we must first create an instance of it. We do this by calling it and passing it a list, then we set this Class instance equal to our_special_class. We can now interact with the Class using the instance name: our_special_class. In Fig. 4 we can see that accessing the Class Attributes is done using the . notation. Notice that Attributes do not have parenthesis or take arguments. Also note: Attributes are NOT immutable, their values can be changed.

Figure 5: Using our Class Method

To use our standard_dev Method we also use the . notation, however because Methods can take arguments, parenthesis are required. This is why those parenthesis show up from time to time, they typically signify that you are using a Method of a Class that requires no inputs. When no parenthesis are present you are typically accessing a Class Attribute.

Why does this matter? Because code can be extremely messy and OOP provides a tidy way of structuring your program. It typically makes code more re-usable and sometimes more readable. It also just looks really cool. The only real way to understand its benefits is to write a few Classes yourself, so go write some code and try it out.

--

--

Jared Delora-Ellefson
Analytics Vidhya

Data Scientist, Mechanical Engineer, Poet, Musician/Producer, DJ