Thursday, January 23, 2014

Object-Oriented Programming

The first three weeks of the course have explored the subject of object-oriented programming. We have been learning about classes, which is what we ended with in CSC108. Personally, the class system is way more enjoyable than learning basic programming skills. Although it was tedious to learn the basics, especially with a decent amount of knowledge prior to taking the course, I'd say that 108 showed me how to create efficient code with enough style to prevent confusion.
Object-oriented programming focuses on the creation and programming of objects. These objects, such as Dog, can be created with the class system, where you can assign to it different attributes inside the __init__ method, like name, type, etc. Within these classes, methods can be implemented to manipulate its attributes. These methods are typical functions, but they are accessible by typing object.method(x) inside the shell. For example, if we have an object Phone, we can write a method Call(self, person) to call a person, producing a string of a conversation. To further understand the meaning of object-oriented programming, a reading from the first week provided a straightforward description: "In procedural programming the focus is on writing functions or procedures which operate on data. In object-oriented programming the focus is on the creation of objects which contain both data and functionality together." Exercise One tied in perfectly with the quote above. The exercise gave us the task to write a Dog object and a Toy object, and showed how their unique methods allow these objects to interact. I prefer object-oriented programming because it feels like I'm actually manipulating something substantial or valuable. While, in procedural programming, it felt like I was inputting arbitrary values into functions, which were often confusing and scattered all over a python file, to see what they would produce. Now that I know the basics of Python programming, I think I'm ready to tackle bigger, more rewarding tasks.

*Notable readings: Object-oriented Python

Week 1-3

To begin my slogging journey of CSC148, here's a recap of the lectures/exercises/labs of the first three weeks (for future reference):
  • The first week's lectures covered the design of new classes. For instance, we analyzed a situation (class Point) to figure out what to call our object, and what methods we needed to implement within the class to manipulate the object. Exercise One from that week, the one with class Dog and class Toy, familiarized me with classes; including how to use the __init__ method to create attributes and how to call other classes' methods.
  • The second week's lectures covered the creation of abstract data types (ADTs). A clever example from the Monday lecture described a Stack ADT as a stack of rocks; where you may add to the stack by adding rocks to the top (push), and you can only remove the rock at the top of the Stack (pop). The week ended with comprehensions and the ternary if, which I will need to look at a bit more to understand them because I honestly didn't pay enough attention in class.
  • The third week's lectures started with the creation of subclasses with the format: class NewClass(OldClass):. Then prof Heap explained how to raise your own exceptions. Finally, he went over how to use the __eq__ method to see if stack objects have the same items in the same order, and the __repr__ method. The __repr__ method was new for me but looked like a fairly simple way to represent a class. The lab for week three helped me understand how to create ADTs such as class Queue, where items come and go at a first come first served basis, rather than the way Stack pushes and pops items.
To conclude my first three weeks of CSC148, I'd say that I'm pleased with my progress. The exercises and labs are actually aiding me a lot and teaching me how to apply the knowledge acquired during the lectures. I'm feeling confident in my abilities with object-oriented programming and I'm excited to see what we learn next! Feel free to comment, you beautiful pythonista/pythoneer.

*Note to self: work on comprehensions, ternary if, exceptions, __repr__ method.