Sunday, 1 February 2015

Week 4: My Impression of Week 4

After a whole weekdays, we had studied so much new knowledge about Python. 

Firstly, I learned the differences between __repr__ and __str__.  __repr__ need to be implemented for any class you implement, sometimes working like 'self' input. Implement __str__ if you think it would be useful to have a string version which give the users information about errors or subclasses and so on. For example, the class named GameView(): then if __repr__ defined to be return GameView(), the input g.__repr__() on shell is a input. However, __str__ will give you information like : 'Requiring a subclass' provided that def __str__(self): return ('Requiring a subclass').

Secondly,  we studied a very important thinking in computer science -- recursion. Recursive thinking is a way that call the function itself to solve a complex and long problems. People maybe wanna ask why we should learn a hard way to code, the answer is that recursion can solve questions in elegant and concise way. On the week 4 lab, we traced a recursive function and write out an unknown doc-string of another recursive function. Nevertheless, the most challenging part is write a recursive function based on the doc_string and examples. I coded that if isinstance(L, list): return max([len(L), length(x) for x in L])   else: return 0. Actually, this function spend me 20 minutes, since it used len() and max() two existing functions so that hard to combine 2 functions. 

Anyway, it is a long way to learn recursion. Studying hard and get good mark. 

No comments:

Post a Comment