Sunday, February 15, 2015

Summary on object oriented programming

After learning about object oriented programming for the past a few weeks, I acquired many skills in designing my own class objects and creating methods for them. Writing my own classes can allow me to create objects that Python does not provide and evaluate them in the way that I want it to work. Although in the beginning it was confusing for me because I do not really know what ‘self’ refers to. I tracked a lot of the example code in the lectures slide into Python visualizer and learned that ‘self’ refers to the class object that I’m creating, whenever I call ‘self.xxx’, Python knows to access the object with the appropriate instance variable, or the appropriate method of that class.

I think object oriented programming allows me to solve problems in a much more efficient manner. Inheritance, for example, can save me a lot of time in writing programs. With inheritance, I can now create objects that use features from another class that I have already written. This is very useful, because for example, I can create a superclass for my general courses, with features like returning course weight, grade achieved etc. With that, I can then create subclasses for my individual courses, without rewriting the same code, if I want to access the weight and grade for the specific course. Thus, object oriented programming extends the types of objects that Python currently provides, which make it handy and applicable to many different situations that a programmer may need.

Also, with enabling classes and instance variables, any client can create the object of the classes and use the methods written for them, without needing to know how the code achieves what it does. Just like I do not know what the code is for Python’s existing methods on its objects, like lists or dictionaries. But all I care is that I can use the append method to add items to lists, and the del method to delete keys in dictionaries. With classes, I can create my own objects for clients to use, with proper docstring provided to show what the methods do and others can write methods on objects that I may need to use. For example, a stack or a queue has very simple but useful features. Although I’m not the one who designed them, I can use their features and solve problems of my own need.


Learning object oriented programming had combined successes and failures. During the process, there were lots of confusion, especially when designing my own classes. I’m sometimes unable to come up with the appropriate instance variables a particular class should have, and the general methods that every subclass might share. However, it is undeniable that object oriented programming is efficient and useful. I hope to continue improve in the future.

Sunday, February 8, 2015

tracing recursion

Looking back at last week, I had mixed successes and failures in understanding the concept of tracing recursive functions. Recursion is something very different from all the functions I used to see. Its function body calls itself, enfolding many layers. Hence, tracing it is like peeling an onion, I have to peel it layer by layer, and in the end, I just cry.

It is very painful in the beginning trying to follow what the function does for each item in the loop. I had difficulties in figuring out what the function produces, if the function is called into itself again. Luckily, the docstrings and descriptions helped me in understanding what the function is aimed at doing. Many recursive functions we have learned in class involve dealing with nested lists, such as getting the sum of all the integers in the nested lists, or the max value for the values in the nested list. As a practice tracing these functions, I gradually observed that there are always a base case and a loop case. Each time iterating the loop, an item is called into the function again, until the base case is reached and returns a value.
However, towards later this week, I noticed that the docstrings start to disappear. Many recursive functions are presented without the coder describing what it does. This is somewhat challenging. I remember in last week’s recursion tracing quiz, the question was a function that didn’t have description, and I did not figure out an answer. Recursive tracing is still quite difficult for me, consider this example below, I remember I had a lot of trouble trying to deduce what it produces.



I have only figured out what S(1) produces, but starting S(2), and later S(3) and more, I’m confused. I remember I asked my friends about this question, and they appear to be similar to my state. Although I knew that I was not alone, I still wanted to understand it. Eventually one of my friends got the answer, S(2) is ([2 * S(2-1)[0] + 2**1 - 1), which is (3, 1). After he explained it, it starts to make more sense to me. I learned that for recursion, if an example is already traced, the result can be directly substituted into another example, if it contains that portion. I think in the future, I would still need to work on recursive tracing more to even get a better grasp of it.

Sunday, February 1, 2015

csc148 week 2 reflection

In this week's study, I learned more methods for classes, such as __init__, __eq__ and more. It was very challenging in the beginning for me, because I find it confusing when I had to refer to instance variables as "self". I had to trace example functions in lecture slides into Python visualizer a couple times before knowing what each variable is referring to.

The next step in learning this week’s material is building classes on top of a superclass. I learned that a general class can be made so that subclasses of it can ‘inherit’ many of its methods for their own use. I find this very useful in solving computer problems in the future, because it saves time for writing duplicate code, when you can just inherit from another class, much like a parent and child relationship.

Also, I learned a little bit of recursive tracing this week. This is something I still need to study on. I’m not quite sure what does each step return at this time. Sometimes I get lost in tracking the nested function. To me, recursion is much like INCEPTION, each time you call it, you go deeper into another layer. I wanted to go to office hour for more clarification; however, I was too busy as I had the first assignment to work on.

I find the first assignment very different from the past assignments I used to do in csc108, because it is so open and personalized. The instructor did not provide any specifications on what methods to implement and what functions should be written; instead, they are all up to the individuals to decide. I spent a long time thinking about the program design, but I was still confused about what each general class should do. To solve this problem, I went to the csc help center twice and talked to the TAs there. They gave me a lot of helpful insights and assisted me in overtaking this challenge.