Sunday, February 1, 2015

The Post

For the first few weeks of the class, we have reviewed about python classes, learned about subclass, and recursive functions. So far I feel more comfortable with python classes than beginning of the semester, but I still have trouble with implementing the recursive functions.

In the beginning of the semester, I did not understand the python classes well. I knew the process of creating and defining classes from CSC108, but I had trouble writing the type contract. For example, storing data. If I wanted to initialize the data for budget, my type contract would be something like (Budget, int) -> nonetype. Since I thought that something has to be returned, I always wrote it like (Budeget, int) -> int. Another thing that I had difficulty was difference between __str__ and __repr__. Since they both return strings, I thought they are identical methods. Then I just implemented __init__ and __str__ in the class, However, when I set a = Budget(12) and type a again in the shell, it returned something like <__main__.Budget object at 0x0000000003038978>. I expected "a" to return "Budget(12)". Then in the lecture next day, I was explained the __str__ was meant for reading purposes and __repr__ was needed so that python evaluates it and returns in string form. I get to have a lot of practice with these methods in assignment 1.

Right now, I do not have a lot of trouble tracing the recursive functions. I can trace the function that is called again and how does it evaluate the inputs. When it comes to implementing it, it gives me some trouble. In the lab 3, there is a question that asked me to make a function from given example. Following the examples and looking at the function I have traced before, I tried it, but it always give back an answer that I did not expect. This might be a sign that I am not tracing recursive function correctly. I should go back and review how recursive functions are working, then I might get an answer that I expected.

No comments:

Post a Comment