Friday, February 28, 2014

Recursion Week 7

      Recursion, the process of repeating items in a self-similar way. Basically, the advantage of recursion is making programming much easier. Let's see an example:
 
By using rec_max(L) inside its own body, this is the recursion. This makes the code be very simple. Moreover, the recursion must have a base part, in this function, is the else x for x in L. If a recursion does not have this part, the recursion will never end.
 
 
Recursion is hard to understand and apply. However, there is no doubt that the recursion is very powerful to make the program simpler, and especially on some big programs.

Inheritance Week 6

      Inheritance is a very important and strong function in connecting two classes. For example, after creating a class, the second class is based on the first one or need some functions from the first one. We can let the second class inherit the first one by writing:

class Second_One(First_One):
    def __init__(self):
        f().__init__(self)

f() is calling all methods in the first class. The advantage of inheritance is that I can only use self.XXX() instead of using First_One.XXX(), which makes my code much simpler obviously.

Raising Exceptions Week 5

      When coding a program, there are some rules, and when the rule is violated, the programmer will raise error to report the situation. For instance, let l be a list of integers with length 5, if I call l[5] in my code, then the index error will be raised.
As for details, usually use:

if XXXX:
    raise XXXXError

and then define the error in the bottom, the error that is raised is an inheritance of Exception class:

class XXXXError(Exception):
    pass

Properties Week 4

      Variables are assigned to some values, for example, let x = 5, then x is a variable and 5 is the value that is assigned to variable x. Variables are stored in the computer's memory, so the property is the permission that allows to make change on or read the values that are stored in the memory.