Wednesday, January 22, 2014

Object-oriented programming

We learnt about hierarchy system in computer science this week. Guess it's everywhere.

Subclass is a great way to start a new class based on some features in existing class. It inherits everything from the parent class. In the meanwhile, new methods can be added and inherited method can be edited.
Every class starts with a __init__ method. And every method starts with the parameter "self", note that we need to put self's type in Quotation Mark. We can also set the a parameter's default value. e.g. data=None. But if we also want to include its type. We put it this way: data: list = None.

In today's class, one of the major issue we discussed is the class __repr__ method, which returned a string that can be used as an instance of the class. We can also the format method to generate the same result: return "Stack({})".format(self._data). Don't forget to put quotation mark because format can only be implemented on str.

Now, let's move on to Exception. This kind of confuse me for quite some time. The example given in class can be also viewed as a hierarchy system where Exception > SpecialException > ExtremeException. Once I put down in a circle graph, I understand better. Because ExtremeException is a subset of SpecialException, together with the fact that the "try, except, except. except" implement in the given sequence. When ExtremeException is raised, it can be caught in SpecialException. Also note that in Exception, the names follow CapitalizedWords rule. 

In a addition, from the notes, we can combine two classes into a master class by class AB(A,B) where A has priority.

BTW one of the problems i had in today's lab is that, keep in mind. every function should have return statement. Though pop method did return something, only when return it can it be shown.

Cheers!


1 comment:

  1. Try to make your next post show a synthesis of your understanding of the topic beyond what you have read or heard in class.

    ReplyDelete