A deeper dive into Python

In my last article, I wrote a basic coder class object as a method for learning python. In this article, I will expand on that illustrating inheritance and more programming concepts that may be a bit different from python. Below I have provided the coder class from my last article with some basic properties and methods. We will be expanding on this with a child class “student”.

We pass the coder class into the student class as an argument in conjunction with calling the super function in the constructor. The super function must be called with the necessary argument/s to create the parent class of “Coder”. This passes the properties and methods from the parent class (Coder) to the child (Student) and allowing us to extend them when using the student class. As we did in the Coder class init function we are passing down the self-referencing argument to be used in creating the cohort property.

The next noticeable difference between Javascript and Python is the use of lambda functions in place of “anonymous” functions. One key difference between the two is that Lambda functions can only return a single expression. It is possible to pass arguments into the lambda function by including the parameter after lambda and before the colon. The below example does not include this and only returns a print function. Moving on to the “say” method we have an illustration of the try and except block. These blocks allow for exception/error handling. In the below method we are attempting to call a lambda function with a key matching the statement parameter. In the case that such a key/value pair does not exist the except block will print a statement instead of the error stopping our program. In addition to these two blocks, there also exists a “finally” block that would execute additional code despite the outcome of the try and except blocks.

Next is the interview method. Here we are demonstrating the ability to pull from the parent class’s properties. The skills list is populated using the learnSkill method of the parent class. First, we check if the length of the list is greater than one in order to format our statement properly. We get the value of the length of our skills list using the “len” function and loop for each index, assuming we have enough values in the list. The range function will return a list of numbers from 0 to the provided number. Over the course of the loop, we construct the output statement formatting the last index of our list differently. Notice that we are able to access the index of each because we instantiated a list that is ordered, unlike a set.

Finally, we get to our crunchTime method which does the inverse of our sleep method by expending sleep credit or accruing sleep debt. As with the method above we are pulling from the parent class’s sleepDebt property. Note the use of bracket notation (instead of dot notation) to access the proper key/value pair. Below that we get to the culmination of all of our coding. When we instantiate our student class we have access to all of the methods and properties of both the parent and child class.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store