In the second week, the class learned how to prevent coord from changing after a point has been created. We used the built in function property to intercept all code that assigns to coord and passes that off to _set_coord. It can also prevent coord from being set twice. It does this by using an if statement that checks if coord has already been set. We also learned that putting an underscore in front of an instance variable such as self.data. For example, self._data makes the instance variable not part of the public interface. Another important thing we learned this week was stacks. A stack is an ADT that has pop and push methods. The push method puts an object at the top a stack. The pop method removes and returns the most recent item pushed to the top of the stack. I found this ADT to be fairly interesting and my friend Kevin found it interesting too.
In the third week, inheritance and exceptions. Inheritance in a way, is to add functionality. The child class extends the functionality of the parent. Thus, a vehicle may have fields for passengers and speed, and a subclass car may have the field engine type. Also, a child class can redefine the behavior of the parent class. Code that is common to many classes can be placed in a parent class too. To declare that we are extending a super class we write "class NewClass(OldClass):" We can add methods and attributes to specialize. We also learned how to raise existing exceptions. To do this we write "raise ValueError." When the line is reached a value error shows up and the code stops running.
In the fourth week, the class learned about recursion. It is a function that calls on itself to accomplish a task. For example, the code "if isinstance(L, list):
return sum([sum_list(x) for x in L])
else: return L" calls on itself if L is a list. The function adds up all the items in a list, including nested lists.
No comments:
Post a Comment