Friday, February 27, 2015

Week 7 - OOP

To begin, we must first understand what Object-oriented programming (OOP) really is. OOP is a programming language model which revolves around objects instead of actions. It also revolves around data instead of logic. OOP is based on the fact that we care about the objects we want to manipulate instead of the logic required to manipulate them.

Some of the basic concepts of OOP are classes, instance variables, inheritance, instance, method and object.

Class: A class is like a blueprint that helps to describe how to make something.
Instance Variables: An instance variable is a variable defined inside a method in a class and only belongs to the instance of the current class.
Inheritance: Inheritance is transfer of characteristics of one class (super-class) to another class (sub-class) which allows the subclass to use the methods from the super-class.
Instance: An instance is an object of a class.
Method: A method is a kind of function defined inside the class
Object: An object is a data structure defined by its class. The object comprises of class variables, instance variables and methods of the class.

Now that the basic terminology has been explained, it is time to actually go through an example of a class (docstrings have been omitted)


1. class Point:
2.  
3.     def __init__(self, coord):    
4.         self.coord = [float(x) for x in coord]
5.
6.     def distance_to_origin(self):
7.         return (sum([x**2 for x in self.coord]))**(1/2)
8.
9.     def __eq__(self, other):
10.        return (isinstance(other, Point) and self.coord == other.coord)



This is an example of a simple class called Point which takes in coordinates in any dimension and has a method that returns the distance to the origin of the coordinate given. 

Now we can go through each line of code explaining how OOP works.

Firstly, on line 1, the class is declared using the keyword class followed by the name Point and a colon.
Next, line 3 contains the special method called __init()__ which is a class initialization method that is called by Python when an instance of the class is created. This method is declared with two underscores before the name and two underscores after the parentheses. Inside the parentheses are the parameters however the difference in OOP is that you must have the first argument in every method called "self". On line 4, in the __init()__ method, a new instance variable is defined called coord which is preceded by the "self" keyword and then a period.
Line 6 is another method declaration, and line 7 returns the distance from the origin to the coordinates passed by the user.
Line 9 is an example of another special method in Python OOP which is also declared by two underscores before the name and two after the parentheses. This example is the eq method which checks if two instances are equal to one another. In this case, on line 10, this method checks if "other" which is a keyword to define the second instance is actually an instance of the class Point and if the coord's passed in are equal.

To declare the class shown above, you can do: 
point1 = Point([3.0, 4.0]).

This object creates an instance of class Point and now the methods in the class can be called using point1. One key thing to note is that in the declaration of __init()__ there are two parameters, "self" and "coord" however only one passed in the object we just created. This is because "self" is a special parameter which users don't need to include themselves.

Now from this object, we can access the other method in the class by doing point1.distance_to_origin() and this would return the float "5.0".

To find out how to use the final special method __eq__ we can create another object called: 
point2 = Point([3.0, 5.0]).

Now you can write "point1 == point2" in Pythons constructor and this would call the __eq__ method in the class. It would return False because even though point2 is an instance of class Point, the coord passed in is not the same.

Therefore, as shown above, these are the basic concepts in Python OOP and using the basic terminology and an example, I have shown how to write an example of a class, declare it and use it in Python.

Friday, February 20, 2015

Week 6

Some of the basic concepts relating to Object-Oriented Programming are classes, instance variables, inheritance, instance, method and object.

Class: A class is like a blueprint that helps to describe how to make something.
Instance Variables: An instance variable is a variable defined inside a method in a class and only belongs to the instance of the current class.
Inheritance: Inheritance is transfer of characteristics of one class (super-class) to another class (sub-class) which allows the subclass to use the methods from the super-class.
Instance: An instance is an object of a class.
Method: A method is a kind of function defined inside the class
Object: An object is a data structure defined by its class. The object comprises of class variables, instance variables and methods of the class.

This is just a brief explanation of terminology that can be very useful when learning about Object-Oriented Programming and without this knowledge, the rest can be quite difficult.







Saturday, February 7, 2015

Week 5

Impressions of Tracing Recursion (From Week 4)

In week 4, we started tracing recursive functions. It was the first time I've traced recursive functions in Python. I really enjoyed the way Prof. Heap explained recursion and the exercises we did in class were a good learning point. They helped me understand how recursion works and also how to trace them very well. I feel like this will be extremely helpful when we start writing our own recursive functions and go more in depth with them. The lab was also helpful because it gave me a chance to test my ability to trace recursive calls and I feel like with all the practice over the last two weeks, that I am getting good at it. Another wonderful example Prof. Heap used while explaining recursion was using the Turtle module to draw a ternary tree recursively. The drawing surprised a lot of people in the class, including myself but when I looked at the code for the function and read it over a few times, the recursive call started to make sense and I found myself tracing over the steps in my mind which was a good thing.