Thursday 23 January 2014

SLOG_Week3_On Object-oriented Programming


It's been quite a challenge for me to pick up a brand new programming language after an entire year absence from coding.  However, Python, a rather handy and clever one, never fails to surprise me as I switch from C++. And "object-oriented programming" could be the first and most familiar concept ever greeted me in this course.

Starting with "class", I retrieved some of the rough impressions about OOP. A a class is a collection of objects containing same type of data and functions. They enjoy an encapsulation thus are independent and reusable. Therefore, compared with procedure oriented programing, OOP enables us to construct, maintain and make changes to a program more easily. For instance, in lab2, we're asked to find a way to shorten the "test time of the Queue class. We locate that the thing to be operated is the dequeue method where "pop(0)" is executed. Then once we replace it with a more efficient one( what can it be?), the new dequeue method is applied to all the objects which are Queues.

The main idea of OOP is likely to get captured, whereas it did took me some time to get used to Python version of class. The sudden appearance of "self.attribute" under the initializing method confused me quite much. After a while I came to realize the attributes seemed to resemble member variables in C++ class. Nevertheless,  the member variables in C++ do not have to be defined in the constructor, but in Python, the attributes should be defined in the initializing method( is that the name for __init__ ?). Meanwhile, the attributes in Python are of the format "self.***", while the name of member variables in C++ can be arbitrary. Besides, in C++, we have to state a member function is private/protected/public, but here in Python, we need only write "self.__***" to get a private attribute or method. Moreover, in C++, defining any constructor or member function of a class could be done outside of the class, provided they've been declared in the class,though I haven't seen such example in Python. ( is it allowed in Python?)

Anyway, may this blog be a good start of my SLOG!