Thursday, February 27, 2014

Week 5 - More Recursion and Namespaces

In our lectures this week, we wrote out code for the recursive solution to the Tower of Hanoi problem, for three stools and any amount of cheeses.  This in-class exercise was great for making sure that I understood how to approach a problem recursively, as well as being an important piece of the Tour.py file we needed to write for Assignment 1.

We also covered Namespaces and Scope this week.  Basically, when an object is created, the name assigned to that object is only valid in the namespace in which it was created.  For example, say we create a variable x within a function test().  Variable x can be used anywhere inside the scope of function test(), but outside of that function, x does not exist.  As a matter of fact, the outside of test() can have its own variable called x, which is completely independent of the x in test().

There are multiple levels of scopes that Python handles, from innermost to outermost:
  • The local scope, which is the "current" function
  • The nonlocal scope, which is the outer functions, if any, of a function.
  • The global scope, which is the module, __main__
  • The outermost scope, which has all of Python's built-in names.
In order to rebind an object, the "nonlocal" or "global" statements can be used.  However, using global variables is not recommended, as they can be difficult to deal with and may easily lead to unexpected behaviour, and from the examples I have looked at, it is pretty easy to understand why they aren't the most intuitive thing to work with.

Week 4 - Recursion

This week of class focused on learning to solve problems using recursive functions.  That is to say, a function that calls itself under specific circumstances.  Deciding when to use a recursive function, as well as how to write it, was pretty tricky at first, especially since it was a very minor part of my high school course, but I was able to get a good grasp on how to work with them.  In a recursive function, you need a general case, and at least one base case.  Recursive functions necessarily take the form of conditional statements, since if there was no condition for the recursive call, the function would repeat itself indefinitely.  The base cases serve as the stopping points for the function, and returning a value instead of repeating the recursion.  The first assignment was talked about, with it being noted that recursion would be necessary in writing the program, so it was good that I had the chance to practice recursion to a level I was comfortable with before I started.

I didn't have any real difficulties with the work this week.  The lab was pretty simple for me and my partner, and the exercise on raising exceptions was straightforward.  There was one thing that was in the lecture that would have been useful in my code for Assignment 1:  The fact that a set of tuples is a valid argument for min.  I ended up using a different approach to the problem that may not have been ideal, but I'll just have to remember that information for the future.

Thursday, January 23, 2014

Object-Oriented Programming

Python, the programming language that we are working with in this course, is an Object-Oriented Programming language.  Object-Oriented Programming, explained in an oversimplified way, focuses on making an object for everything required in a program.  An object can be literally anything, usually corresponding to something in real life, along with relevant attributes and methods.  For example, as we saw as an example in class, we can make an object of a Point on a Cartesian plane, which includes the x and y coordinates as attributes.

Organizing a program in terms of classes and objects has the advantage of making code very easy to follow, as all lines of code related to a specific class and grouped together.  It also allows for easy connections between classes, such as the concept of Inheritance that we have discussed in the course so far, allowing a new subclass, and thus new objects, to behave similarly to its parent class in addition to its own attributes and methods.

Outside of Python I have practiced programming in Visual Basic and a very limited amount of Java, so I do have some experience working with object-oriented programming language, at least on a basic level, and even without having worked on particularly complex programs, I can appreciate how the ability to organize my programs in such an efficient way is very valuable for making it understandable and easy to modify in the future if necessary.  However, there is much more to Object-Oriented Programming that I am unaware of, or less proficient at than I could be, and thus am looking forward to the rest of the course in order to further my understanding of what Object-Oriented Programming has to offer.