Friday, January 31, 2014

Week 4 - Recursion, Testing, and Exercise 2

This week, we looked some more at recursion, which is a very important concept in programming as it can make code easier to read and write, as well as much shorter. As a side note, I find that I really enjoy using shorthands such as list comprehensions and ternary if statements because they shorten code so much and, after understanding their structures, make code much faster to write. From my understanding, recursion is used when our function needs to do the same action (such as one or many calculations) for each part of a whole object. We traced through several example recursive functions in class, and I find it quite easy to understand if I just consider the state of the function at each step and its output.

We also learned about testing our code with unittest, which can provide more specific feedback than doctest. Personally I find writing the unittest functions very tedious, as there are so many cases that can be considered and yet the most of the testing code is relatively similar between the functions. Finding the different cases is the most difficult part. But I do understand the importance of testing code and ensuring that the program works as expected in every situation.


Exercise 2 was due yesterday; it was mainly about inheritance and raising exceptions. I did not find it very difficult, and I had a bit of fun playing around with the inheritance levels and program output. But one thing that I had a problem with was in part b, where the handout stated that “e2b.py” was to import only the exceptions in e2a.py, and not the raiser function. At first, I didn't know that passing raiser in as a function object allowed it to be used as a regular function inside reporter, so I'd thought that we had to pass a string of the function name. This cost me a lot of time because I tried to find out how to convert a string into a function. Afterwards, through some trial and error as well as reading the posts of my fellow classmates on Piazza, I realized my mistake and passed raiser in as an object, then called it in reporter and analyzed its results for e2b.py.

Friday, January 24, 2014

Week 3 - Object-Oriented Programming

Object-Oriented Programming

Python is a programming language that supports object-oriented programming, a coding method which allows the user to simulate the way objects interact in the real world in their code. This method is advantageous in that it can help to clearly write and understand the code used in complex programs. The concept of this method is to create objects that mirror those that exist in reality, and then give them suitable functions to create a program.


From my perspective, the concept of object-oriented programming is an effective way of simplifying programs. Having an object handle a group of tasks like it normally would in the real world makes it much easier for both the writer and future readers of the code to understand how the program works. They could relate the objects and their functions to those of their reality counterparts. I find this concept very interesting, mainly because the way objects model the real-world helps me when planning the code for a program I need to write. Instead of thinking What steps do I need to take to get the desired result? I can now think in terms of What objects are part of the program that I need to write and what do they need to do? This is much more similar to the way I would approach a problem in real life and therefore I find it easier to approach the problem and reach a solution. However, sometimes I find that it's not very clear what those objects should be, or—if two objects are quite similar—which function belongs to which object. I suppose this would require more experience or more careful observation of the way these objects need to interact in order to reach the program requirements.

- Shuqiao Wang