Saturday 28 February 2015

Object Oriented Programming Summary

When we have multiple 'copies' (or instances) of an object, for example an animal, we can use OOP as a way to store each of the data found in each of the objects. We would first create a class which is basically a general 'blueprint' of something whose attributes (or variables) and behavior (which we dictate with methods) are generally defined. Every single instance of this class will contain its own 'version' of this code. For example all animals would have characteristics such as age, height, etc, and have certain general actions such as 'make animal sound' or 'defecate'.

Inheritance
But when we get into more specifics animals, such as a bird, it would undoubtedly still have the characteristics that apply to all animals so there's no need to rewrite the code. In this case we can create a child or subclass of the parent class which would inherit (and be able to use) all the attributes and methods that were made in the parent class. It would extend the parent class. By doing so, we are able to reuse code we have already written which is a key aspect in OOP.
___________________________________________________________________________________
But in this more specific case of the general class, we would have additional variables such as flight speed and methods that are exclusive to this form of animal. We are able to simply add their own behaviors and attributes that are unique to a certain animal. When an instance of a bird is created, it will be BOTH an animal AND a bird and can function/change independently of every other animal or bird too.

Encapsulation
Regarding encapsulation, it is generally used in order to prevent unwanted modification and access to certain parts of our code. In java we would denote variables as private instead of the default public and use accessors and mutators to control how we want our attributes be seen/changed.

But in python there is not true sense of 'private' (Although we can, by convention, use underscores in front of the variable/method name as a representation to others what we don't want them to access). Instead we can use the property() method to control under what conditions we allow a variable to be changed/modified.

Abstraction
Furthermore, in the parent classes we can manage/influence what is required in all of its subclasses. It will create a common interface for all the subclasses that are created so they can all be managed easily. Sometimes it's as simple as denoting a method as abstract or in python we can write the name of a method that we want to be in all subclasses and raise NotImplementedError.

In general Object Oriented Programming allows us to easily create multiple distinct instances of objects that are similar to one another but each contain their own unique characteristics. We could have a single bird or easily initialize multiple instances of birds to create a whole flock of birds with varying ages, feather colours, etc; this allows for easier manipulation of your code and all your objects.

Friday 20 February 2015

Week 5 - Recursion

Recursion is used in order to repeat a similar task a (obviously) finite number of times. More specifically in code, this can be done by having a function to call itself. The thought is to divide up a problem into smaller parts and calculate those individually to finally get the answer as a whole.

For example, let's start with the most basic example of recursion I can think of:

def factorial(number):
    if number == 1:
        return 1
    return number*factorial(number-1)


Nothing complicated here, it was annoying enough to write it in this blog...Maybe next time I'll just put a screenshot of some code in an IDE. That would probably be easier.

The first part would be a base case for when the recursive function will simply return a result without going through any more iterations involving recursion. But before reaching the base case the function would call on itself in order to execute more steps recursively.

I had previously already encountered and learned about recursion so it was not difficult for me to understand it. Different syntax but not much change. I recall when I had first learned about recursion it would definitely help if I would actually write down each step on a piece of paper and go through the results backwards to see how the code works. Now I don't really have to do that with the rather simple examples we learned in class but it still is a good thing to learn. (The exercise/worksheet we did in class was pretty close to what I described but starting with multiple simpler examples instead of going through a more difficult one at the start) All in all recursion is a great concept and tool to learn and even though I know it can be incredibly difficult for some people to grasp at the beginning, at least they can be assured that it will be of great benefit to learn.

Wednesday 18 February 2015

Week 4

I ?think? this and next week has already been marked or something like that but I might as well go back and treat them as practice.

So here we go!

So in CSC148 we started with really the basics of OOP. With classes and basic inheritance which I'll delve into in a later blog. The use of property and the idea of encapsulating the our data to make it 'private'. Then we learned about ADTs as well as started recursion which I have mostly all learned about previously so there were not really any challenges here or new information for me to be learned which might make this post a bit shorter than I would like.

But through learning the implementation and syntax in python (which obviously includes PEP8 regarding the way we format our code/declare new functions and methods, etc...), something new I did learn was for example the __eq__ and the __repr__ functions which were not that hard to understand. It was neat learning about them and the way they are utilized as previously in my experience I hadn't had much of a chance or thought to for example compare 2 objects directly in such a manner

Overall the first few weeks I think were quite a nice review for me of some material as well as learning of new concepts. I do find that some concepts are only briefly mentioned about before moving on such as the small part about encapsulating our data. I honestly would not have remembered learning about it in class if I hadn't written down about 5 lines in my notes regarding it. Anyway here's hoping I'll be able to write my SLOGS on time in the future! (Obviously after catching up.)