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.

Wednesday, January 22, 2014

Week 3

This is the first entry of my sLog.  The instructor asked us to write a paragraph on object-oriented programming.  I haven't really learned much about object-oriented programming before, however, I've seen this phrase many times.  I guess that it is a very fundamental and important topic in programming.  Basically, from my understanding, the object-oriented programming we are doing is very much about creating new object (i.e. creating new class). 


In the first two weeks of CSC148, I've learned some new material regarding object-oriented programming.  We started with design of a new class, which is slightly different comparing to how we did it in CSC108.  The first difference is the header.  We now integrate the type contract into the header.  I started implementing this when writing the first exercise.  This change makes the code look more neat and concise.  


I also learned many more new approaches.  Most of them are not difficult to understand.  One is "list comprehension", which is a very convenient way of writing code, in the form [expression for x in iterable].  With this method, a lot of tedious "for loops" can be eliminated from my codes.  I also found that "isinstance(...)" is super helpful.  For example, 'isinstance(other, Point)' checks if 'other' is an instance of class Point.  I can then easily identify an object in any class I write.  


A very new new topic to me is raising an Exception, and how to create a new class of Exception.  I found that the process of creating a new class is very straightforward.  But the difficult part is to figure out when to raise an exception.  


The most confusing part is the problem with attribute access.  I did not get it during the lecture so I reviewed the slide to understand it.  We put the underline in the variable, (ex. self._coord), which indicates the attribute is "private". The builtin function property then intercepts to avoid modification to coord.  I also discussed with my friend further on this problem as we both found it unclear.