Python objects are an essential tool when programming. It can bring real dynamics to what you are trying to achieve, and broaden the creative scope one can accomplish when writing a script. So I wanted to explain some of the basics, and lay out the case for why coding in python can be one of the most simplest and powerful languages out there.
Python allows us to store data in many different ways. These are known as ‘types’. For example a float is a number followed by decimals, while an integer is a whole number. You can also have strings for sentences, and list to hold multiple types of information within a single variable. A more complicated type are dictionaries or ‘dicts’ for short. These hold a list of key:value information.
student_gpa = {“Stacey”: 2.4, “Mooney”: 3.1, “Karl”: 1.9, ..}
The key being the student’s name and the value their gpa. The values can hold list of more values if you want to get complicated with it.
Class in python is a template for a data type. The blue print of how you will interact with a set parameter of information, and this can be very powerful as it will dictate how a programmer with interact with the information. You’ve probably already dealt with classes before, and not realized it. I’ll let you come to that realization more on your own once all this information settles in.
To set up a class simply call it by using the key word “class” and capitalizing the name.
class StudentGpa:
In order to set the parameters of what to pass into a class I’ll need to initiate my class. For this example I’ll want the student’s name, and a dictionary with the class subject, and exam scores.
To initiate my class I’ll use the __init__ function which uses special double underline on either side. These are known as dunder or magic functions, and worth checking out. Dunder methods look like this:
So any time I create a new instance of this class I will have to pass in a name and a grades variable. You may be wondering about all the selfies. Self simply refers to the class itself, and is assigning all these things to itself (the StudentGpa class). It’s confusing, and repetitive but how else will the class become self aware of it’s own attributes? Hold it’s hand coders because you are the sentient being… for now.
Another great dunder method is the repr method. This simply spits out a string once you instantiate a class.
Now to instantiate a class isto simply activate it by calling on it and assigning it to a variable -as I did with my student Fulano above. The two parameters needed is the name, and my dictionary for his classes and grades.
Great now I can create some custom methods that I would want my class to have. Since this is about calculating the student’s grade let me make one to calculate the average for each subject.
My method iterates through the grades I had already entered when I instantiated my Fulano class, so all I had to do was point to self in the method to access that information. I then called the .grade_average() function on my Fulano class, and presto! I now have an easy way to calculate a students average for any subject they are taking.
The possibilities are endless in terms of what custom methods I want to make, and can really make my life as a teacher so much easier.
These are the basics of classes and Object Oriented Programming!
In my follow up article I’ll talk about class inheritance, and how one class can absorb another class’s characteristics while building its own functions.
Can you think of any reason to do this? What if I wanted to build a class for schools? What information should a school have that would be different than the StudentsGpa class?