This
week, we looked some more at recursion, which is a very important
concept in programming as it can make code easier to read and write,
as well as much shorter. As a side note, I find that I really enjoy
using shorthands such as list comprehensions and ternary if
statements because they shorten code so much and, after understanding
their structures, make code much faster to write. From my
understanding, recursion is used when our function needs to do the
same action (such as one or many calculations) for each part of a
whole object. We traced through several example recursive functions
in class, and I find it quite easy to understand if I just consider
the state of the function at each step and its output.
We
also learned about testing our code with unittest, which can provide
more specific feedback than doctest. Personally I find writing the
unittest functions very tedious, as there are so many cases that can
be considered and yet the most of the testing code is relatively
similar between the functions. Finding the different cases is the
most difficult part. But I do understand the importance of testing
code and ensuring that the program works as expected in every
situation.
Exercise
2 was due yesterday; it was mainly about inheritance and raising
exceptions. I did not find it very difficult, and I had a bit of fun
playing around with the inheritance levels and program output. But
one thing that I had a problem with was in part b, where the handout
stated that “e2b.py” was to import only the exceptions in e2a.py,
and not the raiser function. At first, I didn't know that passing
raiser in as a function object allowed it to be used as a regular
function inside reporter, so I'd thought that we had to pass a string
of the function name. This cost me a lot of time because I tried to
find out how to convert a string into a function. Afterwards, through
some trial and error as well as reading the posts of my fellow
classmates on Piazza, I realized my mistake and passed raiser in as
an object, then called it in reporter and analyzed its results for
e2b.py.