Classes in Python: Fundamentals for Data Scientists | by Erdem Isbilen | Towards Data Science

Classes help us to tidy up our code by encapsulating data and the functions in a single entity. Besides, they increase the reusability and scalability of our code, ease the debugging process.
Once we define a class, we can then use instances of this class to handle specific tasks. In that respect, they are the blueprint of the objects we use in our code.
Let’s understand the basics with a concrete example.
In our example, I will be using Pandas, which is a popular data manipulation library for Python, together with several CSV files containing tabular data.
In Python, the basic class definition has the following format;
class ClassName(): # Comments / Notes ... # Initializer / Instance Attributes ... # Methods ...
To start, let’s create a simple class that helps us to display summary information of the tabular data contained in a CSV file.
class CSVGetInfo:""" This class displays the summary of the tabular data contained in a CSV file """def __init__(self, path, file_name): self.path = path self.file_name = file_namedef display_summary(self): data = pd.read_csv(self.path + self.file_name) print(self.file_name) print(data.info())
In Python, a simple class definition starts with the following notation:
class ClassName:
In our example, our class name is “CSVGetInfo”. Please note that class names in Python follow UpperCaseCamelCase naming convention. Although it is not mandatory, it is common to store the classes in a separate file and import them later when needed.
It is a good practice to add a comment section to let others know what our class does. We can do this by providing our notes inside the triple double-quotes.
class CSVGetInfo:""" This class displays the summary of the tabular data contained in a CSV file """
After these first steps, it is now time to define the data involved with our class. In our example, we need to know the path and file name information of the CSV file we want to read.
So, our class has ‘path’ and ‘file_name’ variables. They will be initialised for every instance of our CSVGetInfo class and they are called as instance variables or attributes of our class.
# Initializer / Instance Attributes  def __init__(self, path, file_name):  self.path = path  self.file_name = file_name
__init__ method indicates what data must be supplied when we create a new instance of our class.
The data supplied with the local variables to be stored in self.path and self.file_name. Here, self represents the instance itself.
Variable initialisation is a mandatory process while creating the instances, and this special __init__ method guarantees that it happens all the time.
Now, we can define functions that use this data to handle specific tasks. Functions in a class structure called as methods or instance functions, as they are tied up every instance of the class. We will define “display_summary” method in our CSVGetInfo class structure, which displays the summary information of tabular data contained in a CSV file.
# Methodsdef display_summary(self): data = pd.read_csv(self.path + self.file_name) print(self.file_name) print(data.info())
Note that we have again used self in our method as a parameter. This is needed to access the data contained in the instance we are working on.
To access the instance variables inside the “display_summary” method, we use self.path and self.file_name notation.
As the class itself is the blueprint of the objects, we need to create instances of the class (objects) to handle the tasks using the methods available in the class definition.
In our class, we have the “display_summary” method.
So let’s create two instances of CSVGetInfo and use “display_summary” method to see what is inside the CSV files.
To create instances/objects of a class, we use the class name and we pass the values to parameters required for the class.
data_by_artists = CSVGetInfo("/Users/erdemisbilen/Lessons/", "data_by_artists.csv")data_by_genres = CSVGetInfo("/Users/erdemisbilen/Lessons/", "data_by_genres.csv")
Now the variable data_by_artist holds the reference to the first instance of our CSVGetInfo class. It is an object/instance of CSVGetInfo class which stores the value of “data_by_artists.csv” in the file_name (instance variable). Other variable data_by_genres holds the reference to the second instance of our class which has “data_by_genres.csv” value stored in the file_name. Although they are created with the same class definition, they have different values stored in the instance variables(attributes).
print(data_by_artists)print(data_by_genres)#Output<__main__.CSVGetInfo instance at 0x10b84f248><__main__.CSVGetInfo instance at 0x114eb58c0>
When we print the instances, we can see that they are stored at different locations in the memory.
Let’s use “display_summary” method.
data_by_artists.display_summary()data_by_genres.display_summary()#Outputdata_by_artists.csv<class 'pandas.core.frame.DataFrame'>RangeIndex: 27606 entries, 0 to 27605Data columns (total 15 columns):artists             27606 non-null objectacousticness        27606 non-null float64danceability        27606 non-null float64duration_ms         27606 non-null float64energy              27606 non-null float64instrumentalness    27606 non-null float64liveness            27606 non-null float64loudness            27606 non-null float64speechiness         27606 non-null float64tempo               27606 non-null float64valence             27606 non-null float64popularity          27606 non-null float64key                 27606 non-null int64mode                27606 non-null int64count               27606 non-null int64dtypes: float64(11), int64(3), object(1)memory usage: 3.2+ MBNonedata_by_genres.csv<class 'pandas.core.frame.DataFrame'>RangeIndex: 2617 entries, 0 to 2616Data columns (total 14 columns):genres              2617 non-null objectacousticness        2617 non-null float64danceability        2617 non-null float64duration_ms         2617 non-null float64energy              2617 non-null float64instrumentalness    2617 non-null float64liveness            2617 non-null float64loudness            2617 non-null float64speechiness         2617 non-null float64tempo               2617 non-null float64valence             2617 non-null float64popularity          2617 non-null float64key                 2617 non-null int64mode                2617 non-null int64dtypes: float64(11), int64(2), object(1)memory usage: 286.3+ KBNone
In this post, I explained the basics of classes in Python.
The code in this post and the CSV files used are available in my GitHub repository.
I hope you found this post useful.
Thank you for reading!