Monday, 16 February 2015

OOP Summary

      We started learning some objected oriented programming in csc108, and have now expanded upon our knowledge of OOP in csc148. I have to admit that learning OOP concepts was no easy task; I've constantly been told that python is a difficult language to start learning about OOP. We have covered a wide range of concepts, and thy are as follows:

1. Classes
I  really struggled to understand what a class was and how all of the pieces are put together in a class through methods, in order to create objects. I was working on Assignment 1, and realized that I couldn't proceed with the shaky understanding that I had. I asked my older brother for help, and he provided me with a great analogy. If we have class Car, for example, the cars are going to be our objects that we want to create. We need to put n number of parts in, specified by the __init__ method, which will be put together by the car factory, which is the constructor. The constructor puts the parts together according to the blueprint in the company safe-this is the class. Then, once everything is assembled according to specifications, a Car object is created. Essentially, a class is like a blueprint for creating objects! I did well on the csc108 OOP part of the exam, but I guess I had lost my understanding over the break. I was proactive, and I asked the right person for help, and now I understand it!

2.Inheritance
Surprisingly, I didn't struggle with inheritance. This was actually one of the easier concepts for me to grasp. We needed to use a lot of inheritance in Assignment 1, so I learned it on my own before we learned it in class. When using classes, there are two ways to relate them (that I know of): inheritance and composition. In composition, one class has an instance variable that holds a reference to another class' object. Inheritance is useful when you want to create an abstract class (the parent) and have the children inherit all of its methods, so that new methods can be added if need be later on. Parent and children classes can act in a few different ways: actions on the child create an action in the parent. For example, if you call a method that isn't explicitly defined in the child class, it will look to use it in the parent class. Actions on the child override those of a parent. A method can be overridden by giving it the same name as the parent, and adding any parameters that you want the child to have. Actions on the child can also extend the actions on a parent. In Assignment 1, we did a lot of overriding, because the general game state and strategy classes could not be implemented due to lack of specificity. Therefore, we needed to raise a NotImplementedError, and have child classes of specific games inherit from the general parent classes, in order to override the methods and create specific, functional ones.

3.Class Variables
 A variable that is shared by all instances of a class, and defined within a class is called a class variable. However, they are defined outside of any class methods. For example :

class Butternubs(object):
    i = 7
    >>> Butternubs.i 
    7
    b = Butternubs()
    >>> b.i   # class variable is accessed as an instance
    7
    >>> b.i = 3 # assignment to the instance
    >>> Butternubs. i # does not change the class variable
    7

4.Instance Variables
Instance variables are defined inside a method and are part of the current instance of a class. An instance is the same thing as an object. For example, "17 is an instance of type int", is the same as saying, "17 is an int object". This was a very good dual definition that I found on stack overflow when I was trying to learn more about instance variables. Whenever you have self.something = thing, self is the instance, and something is the instance variable. I will explain this more when I talk about methods.

5. Methods
A method is essentially a function that is a member of a class; it takes a class instance as its first parameter. The difference between a method and a function, is that a method must be called on an instance. We have been creating our own methods, but there are also special methods that we have been dealing with. __str__, __repr__, __init__, and __eq__ are special methods that we have been utilizing a lot, especially in Assignment 1. The __init__ method basically represents a constructor. When you call Foo(), python creates an object and passes it to the first parameter of the __init__ method, which is self, by convention. I believe that __init__ can be excluded if we don't wish to initialize any class variables. The __eq__ method is pretty straight forward. It checks whether or not two objects are equivalent, based on the code that specifies exactly what makes two objects equivalent. For example, it the game state class of assignment 1, two games were equal if certain things about their game states were equal (i.e. whose turn, is the game over , etc.) I struggled with understanding the difference between __str__ and __repr__ for a long time, but I went online to find some resources, and also worked through the assignment, which helped me a lot. Basically, __str__ returns a string representation of what needs to be displayed to the user (the object). __repr__ returns a string representation of self that produces an equivalent object when evaluated in python. In our class Point from lecture, __repr__ and __str__ would output:
>>> p. __repr__()
Point(3.0, 4.0)
>>>p.__str__()
(3.0 , 4.0)

6.Objects
An object is an abstraction of data. If I remember correctly,  all objects created in python are stored in the Heap. Objects have identities, types, and values. Basically, everything in python is an object. I am still unsure about one thing: is a class an object? I have been  getting mixed answers, and am not sure why. I know that a class contains objects, but this a class can still be an object too, right? Any thoughts are welcome, and are greatly appreciated!

Wel, that's my summary of OOP. I'm sorry about the length; it's a very involved part of programming and I wanted to make sure I covered/understood everything. If anyone has anything to add or correct, please don't hesitate! I like to learn, and know when I've done something wrong!

Sunday, 8 February 2015

Recursion-ursion-sion-n

      For the past couple of weeks we have been learning about a concept that I'm sure is crucial to writing code in computer science; this concept is recursion. As far as I understand, recursion allows a function to call itself. In a recursive function, there is a case that causes the function to recur (call itself), and a base case (when the function produces a result without it recurring). The base case is important because it breaks the recursion/terminates it. I believe this would prevent infinite recursion, and thus stop the program from crashing. We have been tracing through some simple recursive functions, and I notice some differences between recursion and iteration in a loop. Recursion looks a lot more concise, and generally the code looks nicer and more professional. It also looks like recursion is useful when you want to work with sublists, because recursion allows you to access those lists without having to write out complex code, simply by calling the function within itself on each sublist. I find some of the recursive calls confusing. For example, the ones that contain 1 + max([some function(x) for x in L]). I was initially confused about the order of application of max and +1, but I think that I understand it now. I'm going to practice more examples. Tracing recursive code also becomes difficult when there are many sublists, because it's like doing a complex math problem: you have to know your order of operations. One way that recursion is different from other code tracing that we've gone though, stems from the fact that once you've evaluated a sublist or done a recursive call on it, stop tracing it. This is a difficult concept to get used to, since I am used to things being carried over.
      Even though recursion seems like this brilliant, fabulous new concept, I can't help but question the efficacy of its applications sometimes. Surely there must be some downsides to recursion? In the stack, which we are now learning about as well, space is allocated to a function and all of its local variables and arguments (I think) when the program starts. What happens then, if too much recursion is happening inside your function, and the set amount of space that was originally allocated runs out? The program would crash! And as far as I can tell, the program would run more slowly than if you were to implement loop iteration. There must be some way to prevent the stack from overflowing, but I don't believe  python is a language that has any optimizations built in. I sat in on one of my friend's programming languages classes. Dr. Racket was being used, and it had something called tail call optimization built in, in order to prevent the stack from overflowing. I just wanted to connect that idea, because I think it's interesting to apply it to what we're learning! So, with more practice and lessons about recursion, I hope to become better at using it, and learning about more of its applications.

Sunday, 1 February 2015

First Impressions, Anyone?

    I can't believe it's already week four into the course; everything seems like such a blur. We've learned so much new information and there have been so many new experiences, that I don't even know where to begin. I think I'd like to talk about the course lecture style, the material, and the labs.

    As for the lecture, Prof. Danny Heap is a fantastic lecturer; he really knows his stuff. I also appreciate his dry humour; it makes me want to show up for late-evening class. One thing I preferred about CSC108 , though, were the PCRS and the inverse classroom learning style. If you wanted to do well, you were forced to do the weekly exercises and benefit from the material compiled by the profs to demonstrate where exactly you were in your level of understanding. And, with the inverse learning, it was possible to learn the material beforehand, and have it consolidated, reinforced, or cleared up during class. It also allowed you to come to class with specific questions in mind. The labs, in my opinion, are both beneficial and frustrating. On one hand, the labs allow you to work through material that you may not understand with the help and supervision of a TA. That aspect is fantastic; my TA is very nice and the atmosphere is conducive to learning. However, some of the quizzes are too convoluted in their wording, and it takes longer to process what they're saying than it should. This upsets me because I can do the lab exercise without a hitch, but I feel that  the quizzes are not an accurate reflection of my abilities, and they also make me nervous. Because of this, I dread going to the labs, when instead I should be excited about learning. I think for next lab, I will try to do the additional practice problems outside of the lab in order to give myself more time to understand the material. Maybe the case is just that I didn't understand it as well as I thought I did. When I don't understand something, I usually try to figure it out right away by asking my older brother who graduated from computer science; he thinks a lot like me (but is a better programmer!) and this helps me understand the material. I would also like to practice just fooling around with code, like making silly programs and functions that don't do much, aside from teach me where it is appropriate to use certain techniques (ie. while loop or for loop, recursive or not recursive)-the learning possibilities are endless! When I have time to spare I will definitely code for fun.

     Finally, we have learned a lot of new things in these first few weeks. I am new to programming, and so most of the material is new to me, and some of it is difficult to grasp. For example, object oriented programming in python is still a little bit confusing; the concepts self, __str__ and __repr__ still confuse me. I think that self is just meant to represent the instance of whatever you are calling a method on? I don't know if I even explained that well at all-my apologies. And __str__ is meant to return a practical string representation of everything you want to know about an object, whereas __repr__ provides information about your class constructor. We have just started recursion, and so far I understand it, which is a nice change for once! I think that it will get more difficult to trace programs the more complex they get, but for now I find the idea that you can use a function within a function to be very new and exciting! Well, that's all for my first impressions! (for now)