Friday, January 31, 2014

Week 4

We started an important, but difficult topic, recursion.  Professor Heap has mentioned it in the beginning of the term.  A recursion basically loops the function over and over again in itself.  One new approach I learned regarding recursion is ternary if in the form [expression1 if condition else expression2]. This approaches can replace the if/else statements in only one line. But I haven't got used to using this form, I guess that I need to practice using it when I write codes.  The recursion example we did was "sum_list". It sums up nested lists in a list. When writing code for "sum_list", sum_list itself appears in the return statement. Basically, the recursion saves us a lot of work. I hope that I can manage to do recursion by practicing in the future labs.


Another new material was how to extend a class, by using class NewClass(OldClass). We practiced this a lot in the lab. Especially in this week's lab, we tried to write subclass for an existing class, and change some attributes. I'm not familiar with that, so we asked the TA about how to write '__init__' for the new class. I get how to modify or rewrite a superclass function. But the __init__ is still challenging. I will spend more time on it later.


In Exercise 2, we practiced writing our own class of Exceptions. Now I am quite confident with creating and raising Exceptions. When writing the exercise, I was stuck on part(b) for a bit. Then I read the material online and on Piazza, and figured it out by using 'try' and 'except'. I found that Piazza is a very helpful way to get ideas on a specific problem.


In addition, I took some time exploring the difference between '__repr__' and 'str'. I found that '__repr__' is more legit to be used in a new class to represent something, while 'str' is primary purpose is readability.

No comments:

Post a Comment